From ab9fef2ccc6c14e97b3fe83b8aa7a71b71739afd Mon Sep 17 00:00:00 2001 From: shockrah Date: Fri, 5 Feb 2021 16:32:03 -0800 Subject: [PATCH] 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 --- json-api/src/main.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/json-api/src/main.rs b/json-api/src/main.rs index eeeaf04..634790f 100644 --- a/json-api/src/main.rs +++ b/json-api/src/main.rs @@ -12,6 +12,7 @@ extern crate jsonwebtoken; use std::net::SocketAddr; use std::convert::Infallible; // our main dispatcher basically never fails hence why we use this use std::env::{self, set_var}; +use std::collections::HashMap; use tokio; use hyper::{ @@ -45,8 +46,14 @@ const NO_ERR: u16 = 0; const CONFIG_ERR: u16 = 1; const SHUTDOWN_ERR: u16 = 2; -async fn route_dispatcher(pool: &Pool, resp: &mut Response, meth: &Method, path: &str, params: serde_json::Value) { - // At some point we should have some way of hiding this obnoxious complexity +async fn route_dispatcher( + pool: &Pool, + resp: &mut Response, + meth: &Method, + path: &str, + body: Body, + params: HashMap<&str, &str>) { + const GET: &Method = &Method::GET; const POST: &Method = &Method::POST; const DELETE: &Method = &Method::DELETE; @@ -59,7 +66,7 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response, meth: &Method, (POST, routes::CHANNELS_CREATE) => channels::create_channel(pool, resp, params).await, (DELETE, routes::CHANNELS_DELETE) => channels::delete_channel(pool, resp, params).await, /* 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_FROM_ID) =>messages::from_id(pool, resp, params).await, /* ADMIN */ @@ -81,16 +88,20 @@ async fn main_responder(request: Request) -> Result, hyper: use AuthReason::*; 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 path = parts.uri.path(); + let qs = parts.uri.query(); + let params_opt: Option> = 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 Ok(params) = params_res { + if let Some(params) = params_opt { let mysql_pool = Pool::new(&env::var("DATABASE_URL").unwrap()); match auth::wall_entry(path, &mysql_pool, ¶ms).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, NoKey | BadKey => *response.status_mut() = StatusCode::UNAUTHORIZED, ServerIssue(msg) => {