mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-26 07:19:22 +00:00
Start to work on new route system
This commit is contained in:
parent
d660c0d4ba
commit
758774306e
@ -1,7 +1,7 @@
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use crate::constants::admin::AdminRole;
|
use crate::constants::admin::AdminRole;
|
||||||
use crate::controllers::{account_controller, comments_controller, conversations_controller, forez_controller, friends_controller, groups_controller, likes_controller, notifications_controller, posts_controller, push_notifications_controller, search_controller, server_controller, settings_controller, surveys_controller, user_controller, user_ws_controller, virtual_directory_controller, web_app_controller};
|
use crate::controllers::*;
|
||||||
use crate::controllers::admin::*;
|
use crate::controllers::admin::*;
|
||||||
use crate::data::http_request_handler::HttpRequestHandler;
|
use crate::data::http_request_handler::HttpRequestHandler;
|
||||||
use crate::routes::Method::{GET, POST};
|
use crate::routes::Method::{GET, POST};
|
||||||
@ -77,9 +77,6 @@ pub struct Route {
|
|||||||
/// If set to true, unauthenticated requests will be rejected
|
/// If set to true, unauthenticated requests will be rejected
|
||||||
pub need_login: bool,
|
pub need_login: bool,
|
||||||
|
|
||||||
/// The function called to process a request
|
|
||||||
pub func: RequestProcess,
|
|
||||||
|
|
||||||
/// Request rate policy
|
/// Request rate policy
|
||||||
pub limit_policy: LimitPolicy,
|
pub limit_policy: LimitPolicy,
|
||||||
|
|
||||||
@ -88,105 +85,134 @@ pub struct Route {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Route {
|
impl Route {
|
||||||
pub fn get_without_login(uri: &'static str, func: RequestProcess) -> Route {
|
pub fn get_without_login(uri: &'static str) -> Route {
|
||||||
Route {
|
Route {
|
||||||
scope: RouteScope::USER,
|
scope: RouteScope::USER,
|
||||||
method: GET,
|
method: GET,
|
||||||
need_login: false,
|
need_login: false,
|
||||||
uri,
|
uri,
|
||||||
func,
|
|
||||||
limit_policy: LimitPolicy::NONE,
|
limit_policy: LimitPolicy::NONE,
|
||||||
admin_role: None,
|
admin_role: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post_without_login(uri: &'static str, func: RequestProcess) -> Route {
|
pub fn post_without_login(uri: &'static str) -> Route {
|
||||||
Route {
|
Route {
|
||||||
scope: RouteScope::USER,
|
scope: RouteScope::USER,
|
||||||
method: POST,
|
method: POST,
|
||||||
need_login: false,
|
need_login: false,
|
||||||
uri,
|
uri,
|
||||||
func,
|
|
||||||
limit_policy: LimitPolicy::NONE,
|
limit_policy: LimitPolicy::NONE,
|
||||||
admin_role: None,
|
admin_role: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn limited_post_without_login(uri: &'static str, func: RequestProcess, limit_policy: LimitPolicy) -> Route {
|
pub fn limited_post_without_login(uri: &'static str, limit_policy: LimitPolicy) -> Route {
|
||||||
Route {
|
Route {
|
||||||
scope: RouteScope::USER,
|
scope: RouteScope::USER,
|
||||||
method: POST,
|
method: POST,
|
||||||
need_login: false,
|
need_login: false,
|
||||||
uri,
|
uri,
|
||||||
func,
|
|
||||||
limit_policy,
|
limit_policy,
|
||||||
admin_role: None,
|
admin_role: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post(uri: &'static str, func: RequestProcess) -> Route {
|
pub fn post(uri: &'static str) -> Route {
|
||||||
Route {
|
Route {
|
||||||
scope: RouteScope::USER,
|
scope: RouteScope::USER,
|
||||||
method: POST,
|
method: POST,
|
||||||
need_login: true,
|
need_login: true,
|
||||||
uri,
|
uri,
|
||||||
func,
|
|
||||||
limit_policy: LimitPolicy::NONE,
|
limit_policy: LimitPolicy::NONE,
|
||||||
admin_role: None,
|
admin_role: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn limited_post(uri: &'static str, func: RequestProcess, limit_policy: LimitPolicy) -> Route {
|
pub fn limited_post(uri: &'static str, limit_policy: LimitPolicy) -> Route {
|
||||||
Route {
|
Route {
|
||||||
scope: RouteScope::USER,
|
scope: RouteScope::USER,
|
||||||
method: POST,
|
method: POST,
|
||||||
need_login: true,
|
need_login: true,
|
||||||
uri,
|
uri,
|
||||||
func,
|
|
||||||
limit_policy,
|
limit_policy,
|
||||||
admin_role: None,
|
admin_role: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn limited_admin_post_without_login(uri: &'static str, func: RequestProcess, limit_policy: LimitPolicy) -> Route {
|
pub fn limited_admin_post_without_login(uri: &'static str, limit_policy: LimitPolicy) -> Route {
|
||||||
Route {
|
Route {
|
||||||
scope: RouteScope::ADMIN,
|
scope: RouteScope::ADMIN,
|
||||||
method: POST,
|
method: POST,
|
||||||
need_login: false,
|
need_login: false,
|
||||||
uri,
|
uri,
|
||||||
func,
|
|
||||||
limit_policy,
|
limit_policy,
|
||||||
admin_role: None,
|
admin_role: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn admin_post(uri: &'static str, func: RequestProcess) -> Route {
|
pub fn admin_post(uri: &'static str) -> Route {
|
||||||
Route {
|
Route {
|
||||||
scope: RouteScope::ADMIN,
|
scope: RouteScope::ADMIN,
|
||||||
method: POST,
|
method: POST,
|
||||||
need_login: true,
|
need_login: true,
|
||||||
uri,
|
uri,
|
||||||
func,
|
|
||||||
limit_policy: LimitPolicy::NONE,
|
limit_policy: LimitPolicy::NONE,
|
||||||
admin_role: None,
|
admin_role: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn admin_post_restricted(uri: &'static str, func: RequestProcess, role: AdminRole) -> Route {
|
pub fn admin_post_restricted(uri: &'static str, role: AdminRole) -> Route {
|
||||||
Route {
|
Route {
|
||||||
scope: RouteScope::ADMIN,
|
scope: RouteScope::ADMIN,
|
||||||
method: POST,
|
method: POST,
|
||||||
need_login: true,
|
need_login: true,
|
||||||
uri,
|
uri,
|
||||||
func,
|
|
||||||
limit_policy: LimitPolicy::NONE,
|
limit_policy: LimitPolicy::NONE,
|
||||||
admin_role: Some(role),
|
admin_role: Some(role),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
macro_rules! route {
|
||||||
|
($r : expr, $req_uri: expr, $call: expr, GET_NO_LOGIN, $uri: expr, $func: expr)=>{
|
||||||
|
if($uri.eq($req_uri))
|
||||||
|
{
|
||||||
|
$r = Some(Route::get_without_login($uri));
|
||||||
|
|
||||||
|
if let Some(c) = $call {
|
||||||
|
return ($r, Some($func(c)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
($r : expr, $req_uri: expr, $call: expr, POST_NO_LOGIN, $uri: expr, $func: expr)=>{
|
||||||
|
if($uri.eq($req_uri))
|
||||||
|
{
|
||||||
|
$r = Some(Route::post_without_login($uri));
|
||||||
|
|
||||||
|
if let Some(c) = $call {
|
||||||
|
return ($r, Some($func(c)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn find_route(req_uri: &str, call: Option<&mut HttpRequestHandler>) -> (Option<Route>, Option<RequestResult>) {
|
||||||
|
let mut r = None;
|
||||||
|
|
||||||
|
// Server meta routes
|
||||||
|
route!(r, req_uri, call, GET_NO_LOGIN, "/", server_controller::main_index);
|
||||||
|
route!(r, req_uri, call, POST_NO_LOGIN, "/server/config", server_controller::get_config);
|
||||||
|
|
||||||
|
(r, None)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
/// Get the list of routes available
|
/// Get the list of routes available
|
||||||
pub fn get_routes() -> Vec<Route> {
|
pub fn get_routes(route: &str) -> Vec<Route> {
|
||||||
|
|
||||||
vec![
|
vec![
|
||||||
// Server meta routes
|
// Server meta routes
|
||||||
Route::get_without_login("/", Box::new(server_controller::main_index)),
|
Route::get_without_login("/", Box::new(server_controller::main_index)),
|
||||||
@ -401,4 +427,4 @@ pub fn get_routes() -> Vec<Route> {
|
|||||||
Route::admin_post("/admin/users/change_email_address", Box::new(admin_users_controller::change_email_address)),
|
Route::admin_post("/admin/users/change_email_address", Box::new(admin_users_controller::change_email_address)),
|
||||||
Route::admin_post("/admin/users/create_password_reset_link", Box::new(admin_users_controller::create_password_reset_link)),
|
Route::admin_post("/admin/users/create_password_reset_link", Box::new(admin_users_controller::create_password_reset_link)),
|
||||||
]
|
]
|
||||||
}
|
}*/
|
@ -18,7 +18,7 @@ use crate::data::base_request_handler::{BaseRequestHandler, PostFile, RequestVal
|
|||||||
use crate::data::config::Config;
|
use crate::data::config::Config;
|
||||||
use crate::data::http_request_handler::HttpRequestHandler;
|
use crate::data::http_request_handler::HttpRequestHandler;
|
||||||
use crate::helpers::{admin_access_token_helper, admin_key_authentication_challenges_helper, admin_key_registration_challenges_helper, api_helper, requests_limit_helper};
|
use crate::helpers::{admin_access_token_helper, admin_key_authentication_challenges_helper, admin_key_registration_challenges_helper, api_helper, requests_limit_helper};
|
||||||
use crate::routes::{get_routes, RequestResult, Route, RouteScope};
|
use crate::routes::{find_route, RequestResult, Route, RouteScope};
|
||||||
use crate::routes::Method::{GET, POST};
|
use crate::routes::Method::{GET, POST};
|
||||||
use crate::utils::user_data_utils::user_data_path;
|
use crate::utils::user_data_utils::user_data_path;
|
||||||
|
|
||||||
@ -236,7 +236,8 @@ async fn process_simple_route(route: &Route, req: &mut HttpRequestHandler) -> Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let res: RequestResult = (route.func)(req);
|
let (_, res) = find_route(route.uri, Some(req)).await;
|
||||||
|
let res = res.unwrap();
|
||||||
|
|
||||||
requests_limit_helper::trigger_after(res.is_ok(), req, route)?;
|
requests_limit_helper::trigger_after(res.is_ok(), req, route)?;
|
||||||
|
|
||||||
@ -246,31 +247,16 @@ async fn process_simple_route(route: &Route, req: &mut HttpRequestHandler) -> Re
|
|||||||
/// Process an incoming request
|
/// Process an incoming request
|
||||||
async fn process_request(custom_req: CustomRequest) -> HttpResponse {
|
async fn process_request(custom_req: CustomRequest) -> HttpResponse {
|
||||||
let req = &custom_req.req;
|
let req = &custom_req.req;
|
||||||
let routes = get_routes();
|
let (route, _) = find_route(&req.uri().to_string(), None).await;
|
||||||
|
|
||||||
// We search the appropriate route for the request
|
// Check if a route was found with the right verb
|
||||||
let mut route: Option<&Route> = None;
|
if !route.as_ref().map(|r|
|
||||||
for el in &routes {
|
(req.method() == http::Method::GET && r.method == GET) ||
|
||||||
|
(req.method() == http::Method::POST && r.method == POST)
|
||||||
// Check verb
|
).unwrap_or(false) {
|
||||||
if !(req.method() == http::Method::GET && el.method == GET) &&
|
|
||||||
!(req.method() == http::Method::POST && el.method == POST) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check path
|
|
||||||
if !el.uri.eq(req.uri()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
route = Some(el);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if a route was found
|
|
||||||
if let None = route {
|
|
||||||
return HttpResponse::NotFound().json(HttpError::not_found("Route not found!"));
|
return HttpResponse::NotFound().json(HttpError::not_found("Route not found!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let route = route.unwrap();
|
let route = route.unwrap();
|
||||||
|
|
||||||
// Clean requests limit
|
// Clean requests limit
|
||||||
@ -279,7 +265,7 @@ async fn process_request(custom_req: CustomRequest) -> HttpResponse {
|
|||||||
// Execute the request
|
// Execute the request
|
||||||
let mut request = HttpRequestHandler::new(custom_req.req, custom_req.body);
|
let mut request = HttpRequestHandler::new(custom_req.req, custom_req.body);
|
||||||
|
|
||||||
match process_simple_route(route, &mut request).await {
|
match process_simple_route(&route, &mut request).await {
|
||||||
|
|
||||||
// Set default error response if required
|
// Set default error response if required
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user