From 19ad0eee9fab3bf992c700a236bc14b44368ddcc Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 2 Jun 2020 01:32:43 -0700 Subject: [PATCH] OpenAuth + Good both have the same kind of dispatcher response route dispatcher work begins with dispatching to invites::join_invite_code Fixed the non mutable ref borrowing in funcs that required mutable references like most that get called by route_dispatcher --- server/src/main.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index 31c5183..b6a134b 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -22,6 +22,8 @@ use dotenv::dotenv; mod auth; use auth::AuthReason; +mod routes; + fn map_qs(query_string_raw: Option<&str>) -> HashMap<&str, &str> { /* * Parse a given query string and build a hashmap from it @@ -40,12 +42,17 @@ fn map_qs(query_string_raw: Option<&str>) -> HashMap<&str, &str> { map } -async fn route_dispatcher(resp: &Response, meth: &Method, path: &str, params: &HashMap<&str, &str>) { - // we do have to handle some general hyper errors - unimplemented!() +async fn route_dispatcher(resp: &mut Response, meth: &Method, path: &str, params: &HashMap<&str, &str>) { + match (meth, path) { + (&Method::GET, routes::INVITE_JOIN) => invites::join_invite_code(&mut resp, meth, path, params), + _ => { + *resp.status_mut() = StatusCode::NOT_FOUND; + } + } } async fn main_responder(request: Request) -> Result, hyper::Error>{ + use AuthReason::*; let mut response = Response::new(Body::empty()); let method = request.method(); let path = request.uri().path(); @@ -53,11 +60,11 @@ async fn main_responder(request: Request) -> Result, hyper: if let Ok(conn) = Conn::from_url(env::var("DATABASE_URL").unwrap()).await { // some more information in the response would be great right about here - match auth::wall_entry(conn, ¶ms).await { - AuthReason::Good => route_dispatcher(&response, &method, path, ¶ms).await, - AuthReason::LimitPassed => *response.status_mut() = StatusCode::UNAUTHORIZED, - AuthReason::NoKey => *response.status_mut() = StatusCode::UNAUTHORIZED, - AuthReason::InternalFailure => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR + match auth::wall_entry(path, conn, ¶ms).await { + OpenAuth | Good => route_dispatcher(&mut response, &method, path, ¶ms).await, + LimitPassed => *response.status_mut() = StatusCode::UNAUTHORIZED, + NoKey => *response.status_mut() = StatusCode::UNAUTHORIZED, + InternalFailure => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR } } else {