mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-09-25 22:29:45 +00:00
Start to work on new route system
This commit is contained in:
@@ -18,7 +18,7 @@ use crate::data::base_request_handler::{BaseRequestHandler, PostFile, RequestVal
|
||||
use crate::data::config::Config;
|
||||
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::routes::{get_routes, RequestResult, Route, RouteScope};
|
||||
use crate::routes::{find_route, RequestResult, Route, RouteScope};
|
||||
use crate::routes::Method::{GET, POST};
|
||||
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)?;
|
||||
|
||||
@@ -246,31 +247,16 @@ async fn process_simple_route(route: &Route, req: &mut HttpRequestHandler) -> Re
|
||||
/// Process an incoming request
|
||||
async fn process_request(custom_req: CustomRequest) -> HttpResponse {
|
||||
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
|
||||
let mut route: Option<&Route> = None;
|
||||
for el in &routes {
|
||||
|
||||
// Check verb
|
||||
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 {
|
||||
// Check if a route was found with the right verb
|
||||
if !route.as_ref().map(|r|
|
||||
(req.method() == http::Method::GET && r.method == GET) ||
|
||||
(req.method() == http::Method::POST && r.method == POST)
|
||||
).unwrap_or(false) {
|
||||
return HttpResponse::NotFound().json(HttpError::not_found("Route not found!"));
|
||||
}
|
||||
|
||||
let route = route.unwrap();
|
||||
|
||||
// Clean requests limit
|
||||
@@ -279,7 +265,7 @@ async fn process_request(custom_req: CustomRequest) -> HttpResponse {
|
||||
// Execute the request
|
||||
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
|
||||
Err(e) => {
|
||||
|
Reference in New Issue
Block a user