From 5abb162233367f1cbbe7eb048839844137f80c03 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 11 Mar 2022 20:16:02 +0100 Subject: [PATCH] wip --- Cargo.lock | 12 ++++++++++ Cargo.toml | 3 ++- config.yaml | 8 +++---- src/controllers/server_controller.rs | 2 +- src/routes.rs | 35 +++++++++++++++++++++------- src/server.rs | 3 ++- 6 files changed, 47 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0d148ba..ca3c0c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -370,6 +370,17 @@ version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" +[[package]] +name = "async-trait" +version = "0.1.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "061a7acccaa286c011ddc30970520b98fa40e00c9d644633fb26b5fc63a265e3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "attohttpc" version = "0.15.0" @@ -700,6 +711,7 @@ dependencies = [ "actix-multipart", "actix-web", "actix-web-actors", + "async-trait", "bcrypt", "bytes", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 3b020d0..e4db542 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,4 +41,5 @@ zip = "0.5.10" webpage = "1.2.0" gouth = "0.2.0" webauthn-rs = "0.3.2" -url = "2.2.2" \ No newline at end of file +url = "2.2.2" +async-trait = "0.1.52" \ No newline at end of file diff --git a/config.yaml b/config.yaml index e64350b..165ff41 100644 --- a/config.yaml +++ b/config.yaml @@ -14,7 +14,7 @@ server-port: 3000 proxy: "127.0.0.1" # User data storage -storage-url: http://devweb.local/comunic/current/user_data/ +storage-url: http://devweb.internal/comunic/current/user_data/ storage-path: /home/pierre/Documents/projets_web/comunic/current/user_data/ # Specify whether user data files should be made available under the user_data directory @@ -23,8 +23,8 @@ storage-path: /home/pierre/Documents/projets_web/comunic/current/user_data/ serve-storage-files: true # URL where Comunic Terms of use are available -terms-url: http://devweb.local/comunic/current/about.php?cgu -privacy-policy-url: http://devweb.local/comunic/current/about.php?cgu&privacy +terms-url: http://devweb.internal/comunic/current/about.php?cgu +privacy-policy-url: http://devweb.internal/comunic/current/about.php?cgu&privacy # Email where the Comunic staff can be contacted contact-email: contact@communiquons.org @@ -101,4 +101,4 @@ banner: # This option allows to enable some extra features for these groups # # In most cases you should not have to specify this information -forez_groups: 32 \ No newline at end of file +forez_groups: 32 diff --git a/src/controllers/server_controller.rs b/src/controllers/server_controller.rs index 31445e6..9f7a275 100644 --- a/src/controllers/server_controller.rs +++ b/src/controllers/server_controller.rs @@ -8,7 +8,7 @@ use crate::routes::RequestResult; /// @author Pierre Hubert /// Root server index -pub fn main_index(request: &mut HttpRequestHandler) -> RequestResult { +pub async fn main_index(request: &mut HttpRequestHandler) -> RequestResult { request.success("Comunic API server V3. (c) Pierre Hubert 2020") } diff --git a/src/routes.rs b/src/routes.rs index 29c8449..572fae6 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,7 +1,11 @@ use std::error::Error; +use std::future::Future; +use std::pin::Pin; + +use async_trait::async_trait; 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::data::http_request_handler::HttpRequestHandler; use crate::routes::Method::{GET, POST}; @@ -60,6 +64,19 @@ impl LimitPolicy { } } +pub trait RouteTrait{ + fn call(&self, req: &mut HttpRequestHandler) -> Pin>>; +} + +impl RouteTrait for Func where Func: 'static + Fn(&mut HttpRequestHandler) -> Fut + Clone , + Fut: 'static +Future { + #[inline] + #[allow(non_snake_case)] + fn call(&self, req: &mut HttpRequestHandler) -> Pin>> { + Box::pin((self)(req)) + } +} + /// Define types pub type RequestResult = Result<(), Box>; pub type RequestProcess = Box RequestResult>; @@ -78,7 +95,7 @@ pub struct Route { pub need_login: bool, /// The function called to process a request - pub func: RequestProcess, + pub func: Pin>, /// Request rate policy pub limit_policy: LimitPolicy, @@ -88,19 +105,19 @@ pub struct Route { } impl Route { - pub fn get_without_login(uri: &'static str, func: RequestProcess) -> Route { + pub fn get_without_login(uri: &'static str, func: E) -> Route where E: 'static + RouteTrait { Route { scope: RouteScope::USER, method: GET, need_login: false, uri, - func, + func: Box::pin(func), limit_policy: LimitPolicy::NONE, admin_role: None, } } - pub fn post_without_login(uri: &'static str, func: RequestProcess) -> Route { + /*pub fn post_without_login(uri: &'static str, func: RequestProcess) -> Route { Route { scope: RouteScope::USER, method: POST, @@ -182,15 +199,15 @@ impl Route { limit_policy: LimitPolicy::NONE, admin_role: Some(role), } - } + }*/ } /// Get the list of routes available pub fn get_routes() -> Vec { vec![ // Server meta routes - Route::get_without_login("/", Box::new(server_controller::main_index)), - Route::post_without_login("/server/config", Box::new(server_controller::get_config)), + Route::get_without_login("/", server_controller::main_index), + /*Route::post_without_login("/server/config", Box::new(server_controller::get_config)), // Main user WebSocket Route::post("/ws/token", Box::new(user_ws_controller::get_token)), @@ -399,6 +416,6 @@ pub fn get_routes() -> Vec { Route::admin_post("/admin/users/search", Box::new(admin_users_controller::search)), Route::admin_post("/admin/users/info", Box::new(admin_users_controller::get_single)), 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)),*/ ] } \ No newline at end of file diff --git a/src/server.rs b/src/server.rs index 4ac8bd2..c2f5fb7 100644 --- a/src/server.rs +++ b/src/server.rs @@ -236,7 +236,8 @@ async fn process_simple_route(route: &Route, req: &mut HttpRequestHandler) -> Re } - let res: RequestResult = (route.func)(req); + let res = route.func.call(req).await; + requests_limit_helper::trigger_after(res.is_ok(), req, route)?;