1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 00:45:18 +00:00

Make limit system lives

This commit is contained in:
2021-01-23 09:44:34 +01:00
parent dac83ba437
commit ddce4062c7
4 changed files with 116 additions and 16 deletions

View File

@ -34,6 +34,21 @@ pub enum LimitPolicy {
ANY(u64),
}
impl LimitPolicy {
pub fn is_none(&self) -> bool {
matches!(self, LimitPolicy::NONE)
}
pub fn get_count(&self) -> u64 {
match self {
LimitPolicy::NONE => 0,
LimitPolicy::SUCCESS(n) => n.clone(),
LimitPolicy::FAILURE(n) => n.clone(),
LimitPolicy::ANY(n) => n.clone(),
}
}
}
/// Define types
pub type RequestResult = Result<(), Box<dyn Error>>;
pub type RequestProcess = Box<dyn Fn(&mut HttpRequestHandler) -> RequestResult>;

View File

@ -196,6 +196,9 @@ impl FromRequest for CustomRequest {
/// Process a "simple request" aka not a WebSocket request
fn process_simple_route(route: &Route, req: &mut HttpRequestHandler) -> RequestResult {
if requests_limit_helper::trigger_before(req, route).is_err() {
req.too_many_requests("Too many request. Please try again later.")?;
}
// Validate client token
req.check_client_token()?;
@ -205,7 +208,11 @@ fn process_simple_route(route: &Route, req: &mut HttpRequestHandler) -> RequestR
req.check_user_token()?;
}
(route.func)(req)
let res: RequestResult = (route.func)(req);
requests_limit_helper::trigger_after(res.is_ok(), req, route)?;
res
}
/// Process an incoming request