+ 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:
parent
1ca17ec6e0
commit
a941165ea5
@ -58,12 +58,12 @@ async fn route_dispatcher(
|
|||||||
path: &str,
|
path: &str,
|
||||||
body: Body,
|
body: Body,
|
||||||
params: HashMap<String, String>,
|
params: HashMap<String, String>,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap) {
|
||||||
claims: Option<auth::Claim>/* Faster id/perms access from here */) {
|
|
||||||
|
|
||||||
const GET: &Method = &Method::GET;
|
const GET: &Method = &Method::GET;
|
||||||
const POST: &Method = &Method::POST;
|
const POST: &Method = &Method::POST;
|
||||||
const DELETE: &Method = &Method::DELETE;
|
const DELETE: &Method = &Method::DELETE;
|
||||||
|
const PUT: &Method = &Method::PUT;
|
||||||
println!("[HTTP] {}: {}", meth, path);
|
println!("[HTTP] {}: {}", meth, path);
|
||||||
match (meth, path) {
|
match (meth, path) {
|
||||||
/* INVITES */
|
/* INVITES */
|
||||||
@ -90,7 +90,8 @@ async fn route_dispatcher(
|
|||||||
(GET, routes::META) => meta::server_meta(resp).await,
|
(GET, routes::META) => meta::server_meta(resp).await,
|
||||||
/* Federated Routes */
|
/* Federated Routes */
|
||||||
(GET, routes::GET_NEIGHBORS) => meta::server_neighbors(pool, resp).await,
|
(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);
|
println!("[HTTP]\tNOT FOUND: {}: {}", meth, path);
|
||||||
*resp.status_mut() = StatusCode::NOT_FOUND
|
*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 {
|
if let Some(qs) = params_opt {
|
||||||
match auth::wall_entry(path, &DB_POOL, &qs).await {
|
match auth::wall_entry(path, &DB_POOL, &qs).await {
|
||||||
OpenAuth => {
|
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
|
// Only with typical routes do we have to inject the permissions
|
||||||
Good(claim) => {
|
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, Some(claim)).await;
|
route_dispatcher(&DB_POOL, &mut response, &method, path, body, qs, headers).await;
|
||||||
},
|
},
|
||||||
LoginValid(member) => {
|
LoginValid(member) => {
|
||||||
println!("[HTTP] POST /login");
|
println!("[HTTP] POST /login");
|
||||||
|
@ -89,5 +89,30 @@ pub async fn add_neighbor(p: &Pool, response: &mut Response<Body>, body: Body) {
|
|||||||
eprintln!();
|
eprintln!();
|
||||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
*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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ pub const SET_NEW_ADMIN: Rstr = "/owner/newadmin"; // @requiers: ow
|
|||||||
// Server -> Server Routes
|
// Server -> Server Routes
|
||||||
pub const GET_NEIGHBORS: Rstr = "/neighbor/list"; // @requires: none @note must be a member for this list
|
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 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 {
|
pub fn is_open(path: &str) -> bool {
|
||||||
return path.starts_with("/join") || path.starts_with("/meta");
|
return path.starts_with("/join") || path.starts_with("/meta");
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub fn get_pool() -> mysql_async::Pool {
|
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;
|
use mysql_async::Pool;
|
||||||
|
|
||||||
dotenv().ok();
|
|
||||||
return Pool::new(&std::env::var("DATABASE_URL").unwrap())
|
return Pool::new(&std::env::var("DATABASE_URL").unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user