forked from jmug/stoatchat
refactor: move ratelimits to a generic system for all web servers
This commit is contained in:
committed by
Angelo Kontaxis
parent
3a3415915f
commit
fb4011084d
28
crates/services/autumn/src/ratelimits.rs
Normal file
28
crates/services/autumn/src/ratelimits.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use axum::http::{request::Parts, Method};
|
||||
use revolt_ratelimits::ratelimiter::RatelimitResolver;
|
||||
|
||||
pub struct AutumnRatelimits;
|
||||
|
||||
impl RatelimitResolver<Parts> for AutumnRatelimits {
|
||||
fn resolve_bucket<'a>(&self, parts: &'a Parts) -> (&'a str, Option<&'a str>) {
|
||||
let path = parts
|
||||
.uri
|
||||
.path()
|
||||
.trim_matches('/')
|
||||
.split_terminator("/")
|
||||
.collect::<Vec<&str>>();
|
||||
|
||||
match (&parts.method, path.as_slice()) {
|
||||
(&Method::POST, &[tag]) => ("upload", Some(tag)),
|
||||
_ => ("any", None),
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_bucket_limit(&self, bucket: &str) -> u32 {
|
||||
match bucket {
|
||||
"upload" => 10,
|
||||
"any" => u32::MAX,
|
||||
_ => unreachable!("Bucket defined but no limit set"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user