diff options
Diffstat (limited to 'rest/src')
| -rw-r--r-- | rest/src/proxy/mod.rs | 40 | ||||
| -rw-r--r-- | rest/src/ratelimit/mod.rs | 10 |
2 files changed, 35 insertions, 15 deletions
diff --git a/rest/src/proxy/mod.rs b/rest/src/proxy/mod.rs index a684290..ad1abba 100644 --- a/rest/src/proxy/mod.rs +++ b/rest/src/proxy/mod.rs @@ -14,6 +14,10 @@ pub struct ServiceProxy { config: Arc<Config>, } +impl ServiceProxy { + async fn proxy_call() {} +} + impl Service<Request<Body>> for ServiceProxy { type Response = Response<Body>; type Error = hyper::Error; @@ -51,18 +55,32 @@ impl Service<Request<Body>> for ServiceProxy { ); *req.headers_mut() = headers; - let res = self.client - .request(req) - .map_ok(move |res| { - if let Some(bucket) = res.headers().get("x-ratelimit-bucket") { - - println!("bucket ratelimit! {:?} : {:?}", path, bucket); - } + let client = self.client.clone(); + let ratelimiter = self.ratelimiter.clone(); - res - }); - - return Box::pin(res); + return Box::pin(async move { + match ratelimiter.check(&req).await { + Ok(allowed) => match allowed { + true => { + Ok(client + .request(req) + .map_ok(move |res| { + if let Some(bucket) = res.headers().get("x-ratelimit-bucket") { + + println!("bucket ratelimit! {:?} : {:?}", path, bucket); + } + res + }).await.unwrap()) + }, + false => { + Ok(Response::builder().body("ratelimited".into()).unwrap()) + }, + }, + Err(_) => { + Ok(Response::builder().body("server error".into()).unwrap()) + }, + } + }); } } diff --git a/rest/src/ratelimit/mod.rs b/rest/src/ratelimit/mod.rs index c9c7643..07db643 100644 --- a/rest/src/ratelimit/mod.rs +++ b/rest/src/ratelimit/mod.rs @@ -1,4 +1,4 @@ -use common::redis_crate::{AsyncCommands, RedisError, aio::Connection}; +use common::{error::NovaError, redis_crate::{AsyncCommands, RedisError, aio::Connection}}; use hyper::{Body, Request}; use tokio::sync::Mutex; use std::sync::Arc; @@ -15,7 +15,7 @@ impl Ratelimiter { } } - pub async fn check(&mut self,request: Request<Body>) -> bool { + pub async fn check(&self,request: &Request<Body>) -> Result<bool, NovaError> { // we lookup if the route hash is stored in the redis table let path = request.uri().path(); let hash = xxh32(path.as_bytes(), 32); @@ -24,8 +24,10 @@ impl Ratelimiter { let value: Result<String, RedisError> = redis.get(key).await; match value { - Ok(_) => true, - Err(error) => false, + Ok(response) => { + Ok(false) + }, + Err(error) => Err(NovaError::from("failed to issue redis request")), } } } |
