Skip to content

Commit

Permalink
#326: Add tower-http compression as middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
alexohneander committed Aug 26, 2023
1 parent d36d655 commit 76b270b
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 4 deletions.
158 changes: 158 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ uuid = { version = "1", features = ["v4"] }
axum = "0.6.20"
axum-server = { version = "0.5", features = ["tls-rustls"] }
axum-client-ip = "0.4.1"
tower-http = { version= "0.4.3", features = ["full"] }
bencode = { version = "1.0.0-alpha.1", path = "contrib/bencode" }
torrust-tracker-primitives = { version = "3.0.0-alpha.3", path = "packages/primitives" }
torrust-tracker-configuration = { version = "3.0.0-alpha.3", path = "packages/configuration" }
Expand Down
11 changes: 7 additions & 4 deletions src/servers/apis/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use std::sync::Arc;

use axum::{middleware, Router};
use tower_http::compression::CompressionLayer;

use super::v1;
use crate::tracker::Tracker;
Expand All @@ -21,8 +22,10 @@ pub fn router(tracker: Arc<Tracker>) -> Router {

let router = v1::routes::add(prefix, router, tracker.clone());

router.layer(middleware::from_fn_with_state(
tracker.config.clone(),
v1::middlewares::auth::auth,
))
router
.layer(middleware::from_fn_with_state(
tracker.config.clone(),
v1::middlewares::auth::auth,
))
.layer(CompressionLayer::new())
}
2 changes: 2 additions & 0 deletions src/servers/http/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::Arc;
use axum::routing::get;
use axum::Router;
use axum_client_ip::SecureClientIpSource;
use tower_http::compression::CompressionLayer;

use super::handlers::{announce, scrape};
use crate::tracker::Tracker;
Expand All @@ -23,4 +24,5 @@ pub fn router(tracker: Arc<Tracker>) -> Router {
.route("/scrape/:key", get(scrape::handle_with_key).with_state(tracker))
// Add extension to get the client IP from the connection info
.layer(SecureClientIpSource::ConnectInfo.into_extension())
.layer(CompressionLayer::new())
}

0 comments on commit 76b270b

Please sign in to comment.