Removing dynamic routes altogether

This commit is contained in:
shockrah 2020-12-12 14:45:43 -08:00
parent 201297f4c1
commit 4ac696820e
2 changed files with 5 additions and 63 deletions

View File

@ -44,13 +44,13 @@ const SHUTDOWN_ERR: u16 = 2;
async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method, path: &str, params: serde_json::Value) { async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method, path: &str, params: serde_json::Value) {
// At some point we should have some way of hiding this obnoxious complexity // At some point we should have some way of hiding this obnoxious complexity
use routes::resolve_dynamic_route;
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;
match (meth, path) { match (meth, path) {
/* INVITES */ /* INVITES */
(GET, routes::INVITE_CREATE) => invites::create(pool, resp, params).await, (GET, routes::INVITE_CREATE) => invites::create(pool, resp, params).await,
(GET, routes::INVITE_JOIN) => invites::join(pool, resp, params).await,
/* CHANNELS */ /* CHANNELS */
(GET, routes::CHANNELS_LIST) => channels::list_channels(pool, resp).await, (GET, routes::CHANNELS_LIST) => channels::list_channels(pool, resp).await,
(POST, routes::CHANNELS_CREATE) => channels::create_channel(pool, resp, params).await, (POST, routes::CHANNELS_CREATE) => channels::create_channel(pool, resp, params).await,
@ -63,30 +63,12 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method,
(GET, routes::GET_ONLINE_MEMBERS) => members::get_online_members(pool, resp).await, (GET, routes::GET_ONLINE_MEMBERS) => members::get_online_members(pool, resp).await,
/* OWNER */ /* OWNER */
(POST, routes::SET_NEW_ADMIN) => admin::new_admin(pool, resp, params).await, (POST, routes::SET_NEW_ADMIN) => admin::new_admin(pool, resp, params).await,
_ => {
// We attempt dynamic routes as fallback for a few reasons
// 1. theres less of these than there are the static routes
// 2. because of the above and that this is wholly more expensive than static routse
// we can justify putting in tons of branches since we're likely to:
// far jump here, lose cache, and still be be network bound
// Computatinoal bounds are really of no concern with this api since
// we're not doing any heavy calculations at any point
if let Some(route) = resolve_dynamic_route(path) {
match (meth, route.base.as_str()) {
(&Method::GET, routes::DYN_JOIN) => invites::join(pool, resp, params).await,
_ => { _ => {
println!("\tNOT FOUND: {}: {}", meth, path); println!("\tNOT FOUND: {}: {}", meth, path);
*resp.status_mut() = StatusCode::NOT_FOUND *resp.status_mut() = StatusCode::NOT_FOUND
} }
} }
} }
else {
println!("\tNOT FOUND: {}: {}", meth, path);
*resp.status_mut() = StatusCode::NOT_FOUND
}
}
}
}
async fn main_responder(request: Request<Body>) -> Result<Response<Body>, hyper::Error>{ async fn main_responder(request: Request<Body>) -> Result<Response<Body>, hyper::Error>{
use AuthReason::*; use AuthReason::*;

View File

@ -2,6 +2,7 @@
type Rstr = &'static str; type Rstr = &'static str;
pub const INVITE_CREATE: Rstr = "/invite/create"; // @ perms::CREATE_INVITE pub const INVITE_CREATE: Rstr = "/invite/create"; // @ perms::CREATE_INVITE
pub const INVITE_JOIN: Rstr = "/join"; // @ none for new accounts
pub const CHANNELS_LIST: Rstr = "/channels/list"; // requires none pub const CHANNELS_LIST: Rstr = "/channels/list"; // requires none
pub const CHANNELS_CREATE: Rstr = "/channels/create"; // requires @name @kind perms::CREATE_CHANNEl pub const CHANNELS_CREATE: Rstr = "/channels/create"; // requires @name @kind perms::CREATE_CHANNEl
@ -16,48 +17,7 @@ pub const GET_ONLINE_MEMBERS: Rstr = "/members/get_online";
pub const SET_PERMS_BY_ADMIN: Rstr = "/admin/setpermisions"; // @requires perms::ADMIN pub const SET_PERMS_BY_ADMIN: Rstr = "/admin/setpermisions"; // @requires perms::ADMIN
pub const SET_NEW_ADMIN: Rstr = "/owner/newadmin"; // @requiers: owner perms pub const SET_NEW_ADMIN: Rstr = "/owner/newadmin"; // @requiers: owner perms
// TODO: here be dragons...
// potentially adding more bases later
pub const DYNAMIC_ROUTE_BASES: [(Rstr, bool);3] = [
("/join", true), // open
("/public", true), // open : valid sections := neighbors|rules|description
("/user", false), // TODO: valid sections := /meta/<name>|/dm/<name>
];
pub const DYN_JOIN: Rstr = DYNAMIC_ROUTE_BASES[0].0;
pub struct DynRoute {
pub base: String,
pub dynamic: String,
}
pub fn resolve_dynamic_route(uri: &str) -> Option<DynRoute> {
let mut valid = false;
let mut base_ref = "";
for base in DYNAMIC_ROUTE_BASES.iter() {
if uri.starts_with(base.0) {
valid = true;
base_ref = base.0;
break;
}
}
if valid {
Some(DynRoute {
base: base_ref.into(),
dynamic: uri.to_string().replace(base_ref, "")
})
}
else {
None
}
}
pub fn is_open(path: &str) -> bool { pub fn is_open(path: &str) -> bool {
/* return path.starts_with("/join") || path.starts_with("/meta");
* Simple interface for determining if a route/base is open
* i.e. requires authentication or not
*/
return path.starts_with("/join/");
} }