Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#326: Add tower-http compression as middleware #385

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice not to include the full tower-http feature set, this will increase build times.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are the different feature-sets to select from: https://github.com/tower-rs/tower-http/blob/master/tower-http/Cargo.toml#L55

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@da2ce7 You are absolutely right.
I have adjusted it. I will create a new pull-request.

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())
}