From a941165ea576c86ea6ed3423f2a87aaca811fee6 Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 11 May 2021 17:25:51 -0700 Subject: [PATCH] + adding /neighbor/update route to dispatch ! This route needs way more testing as its currently failing the prelim test listed right now ! Some more misc changes in testing/mod.rs that aren't imporant at all to anyone It's just an extra comment --- json-api/src/main.rs | 13 +++++++------ json-api/src/meta.rs | 25 +++++++++++++++++++++++++ json-api/src/routes.rs | 1 + json-api/src/testing/mod.rs | 4 ++-- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/json-api/src/main.rs b/json-api/src/main.rs index 199eb5a..2ff3e16 100644 --- a/json-api/src/main.rs +++ b/json-api/src/main.rs @@ -58,12 +58,12 @@ async fn route_dispatcher( path: &str, body: Body, params: HashMap, - headers: HeaderMap, - claims: Option/* Faster id/perms access from here */) { + headers: HeaderMap) { const GET: &Method = &Method::GET; const POST: &Method = &Method::POST; const DELETE: &Method = &Method::DELETE; + const PUT: &Method = &Method::PUT; println!("[HTTP] {}: {}", meth, path); match (meth, path) { /* INVITES */ @@ -90,7 +90,8 @@ async fn route_dispatcher( (GET, routes::META) => meta::server_meta(resp).await, /* Federated Routes */ (GET, routes::GET_NEIGHBORS) => meta::server_neighbors(pool, resp).await, - (POST, routes::ADD_NEIGHBOR) => meta::add_neighbor(pool, resp, params).await, + (POST, routes::ADD_NEIGHBOR) => meta::add_neighbor(pool, resp, body).await, + (PUT, routes::UPDATE_NEIGHBOR) => meta::update_neighbor(pool, resp, params, body).await, _ => { println!("[HTTP]\tNOT FOUND: {}: {}", meth, path); *resp.status_mut() = StatusCode::NOT_FOUND @@ -116,11 +117,11 @@ async fn main_responder(request: Request) -> Result, hyper: if let Some(qs) = params_opt { match auth::wall_entry(path, &DB_POOL, &qs).await { OpenAuth => { - route_dispatcher(&DB_POOL, &mut response, &method, path, body, qs, headers, None).await; + route_dispatcher(&DB_POOL, &mut response, &method, path, body, qs, headers).await; }, // Only with typical routes do we have to inject the permissions - Good(claim) => { - route_dispatcher(&DB_POOL, &mut response, &method, path, body, qs, headers, Some(claim)).await; + Good(_claim/* TODO: start moving route handlers to use claims.id for faster access */) => { + route_dispatcher(&DB_POOL, &mut response, &method, path, body, qs, headers).await; }, LoginValid(member) => { println!("[HTTP] POST /login"); diff --git a/json-api/src/meta.rs b/json-api/src/meta.rs index 54571c5..3f12b8c 100644 --- a/json-api/src/meta.rs +++ b/json-api/src/meta.rs @@ -89,5 +89,30 @@ pub async fn add_neighbor(p: &Pool, response: &mut Response, body: Body) { eprintln!(); *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; } + } else { + *response.status_mut() = StatusCode::BAD_REQUEST; + } +} + +pub async fn update_neighbor(p: &Pool, response: &mut Response, params: HashMap, body: Body) { + // First collect the target url from the map and try to parse the body + let target = params.get("url"); + let body: Bytes = to_bytes(body).await.unwrap_or(Bytes::new()); + let s: String = String::from_utf8_lossy(&body).to_string(); + let json: JsonResult = serde_json::from_str(&s); + + println!("\tjson result: {:?}", json); + println!("\tbody {}", s); + // Verify query string parameter **and** body before attempting to reach database + match (target, json) { + (Some(target_url), Ok(neighbor)) => { + // Only return a 500 if something happened on db-lib's end + if let Err(e) = db::neighbors::update(p, target_url, neighbor).await { + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; + eprintln!("/neighbor/update [DB-LIB] {}", e); + } + // Nothing to do on success 200 is already set by hyper + }, + _ => *response.status_mut() = StatusCode::BAD_REQUEST } } diff --git a/json-api/src/routes.rs b/json-api/src/routes.rs index d391ffe..b0670e1 100644 --- a/json-api/src/routes.rs +++ b/json-api/src/routes.rs @@ -27,6 +27,7 @@ pub const SET_NEW_ADMIN: Rstr = "/owner/newadmin"; // @requiers: ow // Server -> Server Routes pub const GET_NEIGHBORS: Rstr = "/neighbor/list"; // @requires: none @note must be a member for this list pub const ADD_NEIGHBOR: Rstr = "/neighbor/add"; // @requires: perm::add_neighbor +pub const UPDATE_NEIGHBOR: Rstr = "/neighbor/update"; // @requires perms::add_neighbor @url(unique) pub fn is_open(path: &str) -> bool { return path.starts_with("/join") || path.starts_with("/meta"); diff --git a/json-api/src/testing/mod.rs b/json-api/src/testing/mod.rs index 38e4837..340b9de 100644 --- a/json-api/src/testing/mod.rs +++ b/json-api/src/testing/mod.rs @@ -3,10 +3,10 @@ #[cfg(test)] pub fn get_pool() -> mysql_async::Pool { - use dotenv::dotenv; + // NOTE: this assumes that DATABASE_URL has been set in the environment + // prior to running use mysql_async::Pool; - dotenv().ok(); return Pool::new(&std::env::var("DATABASE_URL").unwrap()) }