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
This commit is contained in:
shockrah 2020-06-02 01:32:43 -07:00
parent 68aeb50175
commit 19ad0eee9f

View File

@ -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<Body>, 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<Body>, 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<Body>) -> Result<Response<Body>, 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<Body>) -> Result<Response<Body>, 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, &params).await {
AuthReason::Good => route_dispatcher(&response, &method, path, &params).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, &params).await {
OpenAuth | Good => route_dispatcher(&mut response, &method, path, &params).await,
LimitPassed => *response.status_mut() = StatusCode::UNAUTHORIZED,
NoKey => *response.status_mut() = StatusCode::UNAUTHORIZED,
InternalFailure => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
}
}
else {