route_dispatcher now reflects new hashmap usage over serde_json::Value's

HashMap of query string parameters is generated in main_responder before passed on
This commit is contained in:
shockrah 2021-02-05 16:32:03 -08:00
parent 33ae768ae4
commit ab9fef2ccc

View File

@ -12,6 +12,7 @@ extern crate jsonwebtoken;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::convert::Infallible; // our main dispatcher basically never fails hence why we use this use std::convert::Infallible; // our main dispatcher basically never fails hence why we use this
use std::env::{self, set_var}; use std::env::{self, set_var};
use std::collections::HashMap;
use tokio; use tokio;
use hyper::{ use hyper::{
@ -45,8 +46,14 @@ const NO_ERR: u16 = 0;
const CONFIG_ERR: u16 = 1; const CONFIG_ERR: u16 = 1;
const SHUTDOWN_ERR: u16 = 2; const SHUTDOWN_ERR: u16 = 2;
async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method, path: &str, params: serde_json::Value) { async fn route_dispatcher(
// At some point we should have some way of hiding this obnoxious complexity pool: &Pool,
resp: &mut Response<Body>,
meth: &Method,
path: &str,
body: Body,
params: HashMap<&str, &str>) {
const GET: &Method = &Method::GET; const GET: &Method = &Method::GET;
const POST: &Method = &Method::POST; const POST: &Method = &Method::POST;
const DELETE: &Method = &Method::DELETE; const DELETE: &Method = &Method::DELETE;
@ -59,7 +66,7 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method,
(POST, routes::CHANNELS_CREATE) => channels::create_channel(pool, resp, params).await, (POST, routes::CHANNELS_CREATE) => channels::create_channel(pool, resp, params).await,
(DELETE, routes::CHANNELS_DELETE) => channels::delete_channel(pool, resp, params).await, (DELETE, routes::CHANNELS_DELETE) => channels::delete_channel(pool, resp, params).await,
/* MESSAGING */ /* MESSAGING */
(POST, routes::MESSAGE_SEND) => messages::send_message(pool, resp, params).await, (POST, routes::MESSAGE_SEND) => messages::send_message(pool, resp, body, params).await,
(GET, routes::MESSAGE_TIME_RANGE) => messages::get_by_time(pool, resp, params).await, (GET, routes::MESSAGE_TIME_RANGE) => messages::get_by_time(pool, resp, params).await,
(GET, routes::MESSAGE_FROM_ID) =>messages::from_id(pool, resp, params).await, (GET, routes::MESSAGE_FROM_ID) =>messages::from_id(pool, resp, params).await,
/* ADMIN */ /* ADMIN */
@ -81,16 +88,20 @@ async fn main_responder(request: Request<Body>) -> Result<Response<Body>, hyper:
use AuthReason::*; use AuthReason::*;
let mut response = Response::new(Body::empty()); let mut response = Response::new(Body::empty());
let (parts, mut body) = request.into_parts(); let (parts, body) = request.into_parts();
let method = parts.method; let method = parts.method;
let path = parts.uri.path(); let path = parts.uri.path();
let qs = parts.uri.query();
let params_opt: Option<HashMap<&str, &str>> = if let Some(query_string) = qs {
Some(http::parse_query_string(query_string))
} else {
None
};
let params_res = http::parse_json_params(&mut body).await; if let Some(params) = params_opt {
if let Ok(params) = params_res {
let mysql_pool = Pool::new(&env::var("DATABASE_URL").unwrap()); let mysql_pool = Pool::new(&env::var("DATABASE_URL").unwrap());
match auth::wall_entry(path, &mysql_pool, &params).await { match auth::wall_entry(path, &mysql_pool, &params).await {
OpenAuth | Good => route_dispatcher(&mysql_pool, &mut response, &method, path, params).await, OpenAuth | Good => route_dispatcher(&mysql_pool, &mut response, &method, path, body, params).await,
LoginValid => auth::login_get_jwt(&mysql_pool, &mut response, params).await, LoginValid => auth::login_get_jwt(&mysql_pool, &mut response, params).await,
NoKey | BadKey => *response.status_mut() = StatusCode::UNAUTHORIZED, NoKey | BadKey => *response.status_mut() = StatusCode::UNAUTHORIZED,
ServerIssue(msg) => { ServerIssue(msg) => {