mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-03 17:14:03 +00:00 
			
		
		
		
	Make limit system lives
This commit is contained in:
		@@ -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>;
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user