summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.circleci/config.yml11
-rw-r--r--.devcontainer/devcontainer.json2
-rw-r--r--common/rust/src/error.rs6
-rw-r--r--rest/src/proxy/mod.rs40
-rw-r--r--rest/src/ratelimit/mod.rs10
-rw-r--r--webhook/src/main.rs1
6 files changed, 51 insertions, 19 deletions
diff --git a/.circleci/config.yml b/.circleci/config.yml
index 5ca796b..1189563 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -36,6 +36,9 @@ jobs:
name: "Test"
command: "bazelisk test //:tests || true"
+ - store_artifacts:
+ path: ~/project/bazel-testlogs
+
- store_test_results:
path: ~/project/bazel-testlogs/rest
- store_test_results:
@@ -74,6 +77,9 @@ jobs:
- store_test_results:
path: ~/project/bazel-testlogs/webhook
+ - store_artifacts:
+ path: ~/project/bazel-testlogs
+
- run:
name: "Build"
command: "bazelisk build //:packages"
@@ -103,13 +109,14 @@ jobs:
- run:
name: "Test"
command: |
- $ErrorActionPreference = ‘SilentlyContinue’
- bazelisk test //:tests
+ bazelisk test //:tests | Out-Null
- store_test_results:
path: ~/project/bazel-testlogs/rest
- store_test_results:
path: ~/project/bazel-testlogs/webhook
+ - store_artifacts:
+ path: ~/project/bazel-testlogs
- run:
name: "Build"
diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json
index 03c3e4c..a962bc7 100644
--- a/.devcontainer/devcontainer.json
+++ b/.devcontainer/devcontainer.json
@@ -36,5 +36,5 @@
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",
"overrideCommand": false,
- "runArgs": ["--init"],
+ "runArgs": ["--init", "--network=host"]
} \ No newline at end of file
diff --git a/common/rust/src/error.rs b/common/rust/src/error.rs
index be1607a..1a24657 100644
--- a/common/rust/src/error.rs
+++ b/common/rust/src/error.rs
@@ -10,3 +10,9 @@ impl fmt::Display for NovaError {
write!(f, "An error occurred within the nova system: {}", self.message) // user-facing output
}
}
+
+impl From<&str> for NovaError {
+ fn from(message: &str) -> Self {
+ NovaError { message: message.to_string() }
+ }
+} \ No newline at end of file
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")),
}
}
}
diff --git a/webhook/src/main.rs b/webhook/src/main.rs
index eef4751..98e5f13 100644
--- a/webhook/src/main.rs
+++ b/webhook/src/main.rs
@@ -15,7 +15,6 @@ async fn main() {
}
async fn start(settings: Settings<Config>) {
-
let addr = format!(
"{}:{}",
settings.config.server.address, settings.config.server.port