1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00
This commit is contained in:
Pierre HUBERT 2022-03-11 20:16:02 +01:00
parent 254b74282e
commit 5abb162233
6 changed files with 47 additions and 16 deletions

12
Cargo.lock generated
View File

@ -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",

View File

@ -42,3 +42,4 @@ webpage = "1.2.0"
gouth = "0.2.0"
webauthn-rs = "0.3.2"
url = "2.2.2"
async-trait = "0.1.52"

View File

@ -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

View File

@ -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")
}

View File

@ -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<Box<dyn Future<Output=RequestResult>>>;
}
impl<Func, Fut> RouteTrait for Func where Func: 'static + Fn(&mut HttpRequestHandler) -> Fut + Clone ,
Fut: 'static +Future<Output=RequestResult> {
#[inline]
#[allow(non_snake_case)]
fn call(&self, req: &mut HttpRequestHandler) -> Pin<Box<dyn Future<Output=RequestResult>>> {
Box::pin((self)(req))
}
}
/// Define types
pub type RequestResult = Result<(), Box<dyn Error>>;
pub type RequestProcess = Box<dyn Fn(&mut HttpRequestHandler) -> 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<Box<dyn RouteTrait + 'static>>,
/// 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<E>(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<Route> {
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> {
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)),*/
]
}

View File

@ -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)?;