+ 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
This commit is contained in:
shockrah 2021-05-11 17:25:51 -07:00
parent 1ca17ec6e0
commit a941165ea5
4 changed files with 35 additions and 8 deletions

View File

@ -58,12 +58,12 @@ async fn route_dispatcher(
path: &str,
body: Body,
params: HashMap<String, String>,
headers: HeaderMap,
claims: Option<auth::Claim>/* 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<Body>) -> Result<Response<Body>, 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");

View File

@ -89,5 +89,30 @@ pub async fn add_neighbor(p: &Pool, response: &mut Response<Body>, 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<Body>, params: HashMap<String, String>, 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<Neighbor> = 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
}
}

View File

@ -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");

View File

@ -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())
}