Skip to content

Commit 7fa1eb3

Browse files
committed
feat(anda_engine_server): add CompressionMiddleware
1 parent b856552 commit 7fa1eb3

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ isolang = { version = "2.4", features = [
101101
"local_names",
102102
"list_languages",
103103
] }
104+
tower-http = { version = "0.6", features = ["compression-gzip"] }
104105

105106
# [patch.crates-io]
106107
# candid = { git = "https://github.com/ldclabs/candid.git", rev = "4cf7d02bad9530172cb4cafe733cb1e80689b793" } # remove check_recursion on stack for TEE

anda_engine_server/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "anda_engine_server"
33
description = "A http server to serve multiple Anda engines."
44
repository = "https://github.com/ldclabs/anda/tree/main/anda_engine_server"
55
publish = true
6-
version = "0.9.9"
6+
version = "0.9.10"
77
edition.workspace = true
88
keywords.workspace = true
99
categories.workspace = true
@@ -26,5 +26,6 @@ tokio = { workspace = true }
2626
log = { workspace = true }
2727
ic_auth_types = { workspace = true }
2828
ic_auth_verifier = { workspace = true, features = ["full"] }
29+
tower-http = { workspace = true }
2930

3031
[dev-dependencies]

anda_engine_server/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use tokio::signal;
88
use tokio_util::sync::CancellationToken;
99

1010
mod handler;
11-
mod middleware;
11+
pub mod middleware;
1212
mod types;
1313

1414
use handler::*;
15-
pub use middleware::{ApiKeyMiddleware, HttpMiddleware};
15+
use middleware::*;
1616

1717
const APP_NAME: &str = env!("CARGO_PKG_NAME");
1818
const APP_VERSION: &str = env!("CARGO_PKG_VERSION");

anda_engine_server/src/middleware.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@ use axum::{
66
response::{IntoResponse, Response},
77
};
88
use std::{future::Future, sync::Arc};
9+
use tower_http::compression::CompressionLayer;
910

10-
use crate::handler::AppState;
11+
pub use crate::handler::AppState;
12+
13+
pub type AppRouter = Router<AppState>;
1114

1215
/// Object-safe middleware trait for applying HTTP middleware to the server `Router`.
1316
///
1417
/// This is intentionally type-erased so callers can register arbitrary axum/tower
1518
/// middleware without turning `ServerBuilder` into a giant generic type.
1619
pub trait HttpMiddleware: Send + Sync + 'static {
17-
fn apply(&self, router: Router<AppState>) -> Router<AppState>;
20+
fn apply(&self, router: AppRouter) -> AppRouter;
1821
}
1922

2023
impl<F> HttpMiddleware for F
2124
where
22-
F: Fn(Router<AppState>) -> Router<AppState> + Send + Sync + 'static,
25+
F: Fn(AppRouter) -> AppRouter + Send + Sync + 'static,
2326
{
24-
fn apply(&self, router: Router<AppState>) -> Router<AppState> {
27+
fn apply(&self, router: AppRouter) -> AppRouter {
2528
(self)(router)
2629
}
2730
}
@@ -42,11 +45,36 @@ where
4245
F: Fn(Request, Next) -> Fut + Clone + Send + Sync + 'static,
4346
Fut: Future<Output = Response> + Send + 'static,
4447
{
45-
fn apply(&self, router: Router<AppState>) -> Router<AppState> {
48+
fn apply(&self, router: AppRouter) -> AppRouter {
4649
router.layer(axum::middleware::from_fn(self.f.clone()))
4750
}
4851
}
4952

53+
/// Middleware that applies response compression using `tower-http`'s `CompressionLayer`.
54+
pub struct CompressionMiddleware {
55+
layer: CompressionLayer,
56+
}
57+
58+
impl CompressionMiddleware {
59+
pub fn new(layer: CompressionLayer) -> Self {
60+
Self { layer }
61+
}
62+
}
63+
64+
impl Default for CompressionMiddleware {
65+
fn default() -> Self {
66+
Self {
67+
layer: CompressionLayer::new(),
68+
}
69+
}
70+
}
71+
72+
impl HttpMiddleware for CompressionMiddleware {
73+
fn apply(&self, router: AppRouter) -> AppRouter {
74+
router.layer(self.layer.clone())
75+
}
76+
}
77+
5078
/// A simple API key middleware that validates `x-api-key` on every request.
5179
///
5280
/// Use `exempt_path` to allow unauthenticated endpoints (e.g. health/info routes).
@@ -72,7 +100,7 @@ impl ApiKeyMiddleware {
72100
}
73101

74102
impl HttpMiddleware for ApiKeyMiddleware {
75-
fn apply(&self, router: Router<AppState>) -> Router<AppState> {
103+
fn apply(&self, router: AppRouter) -> AppRouter {
76104
let expected_key = self.expected_key.clone();
77105
let exempt_paths = self.exempt_paths.clone();
78106

0 commit comments

Comments
 (0)