From bf3f66f6d2c920bf2f47e03d2a6bee96ef124726 Mon Sep 17 00:00:00 2001 From: Alex Wellnitz Date: Sun, 27 Aug 2023 01:05:11 +0200 Subject: [PATCH] #262: Add tower-http compression middleware --- Cargo.lock | 42 ++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 +- src/web/api/v1/routes.rs | 3 ++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8b1bca63..3f61892a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,7 +70,7 @@ dependencies = [ "tokio", "tokio-util", "tracing", - "zstd", + "zstd 0.12.3+zstd.1.5.2", ] [[package]] @@ -348,6 +348,22 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" +[[package]] +name = "async-compression" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942c7cd7ae39e91bde4820d74132e9862e62c2f386c3aa90ccf55949f5bad63a" +dependencies = [ + "brotli", + "flate2", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", + "zstd 0.11.2+zstd.1.5.2", + "zstd-safe 5.0.2+zstd.1.5.2", +] + [[package]] name = "async-trait" version = "0.1.69" @@ -3198,6 +3214,7 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8bd22a874a2d0b70452d5597b12c537331d49060824a95f49f108994f94aa4c" dependencies = [ + "async-compression", "bitflags 2.3.3", "bytes", "futures-core", @@ -3206,6 +3223,8 @@ dependencies = [ "http-body", "http-range-header", "pin-project-lite", + "tokio", + "tokio-util", "tower-layer", "tower-service", ] @@ -3726,13 +3745,32 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" +[[package]] +name = "zstd" +version = "0.11.2+zstd.1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20cc960326ece64f010d2d2107537f26dc589a6573a316bd5b1dba685fa5fde4" +dependencies = [ + "zstd-safe 5.0.2+zstd.1.5.2", +] + [[package]] name = "zstd" version = "0.12.3+zstd.1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76eea132fb024e0e13fd9c2f5d5d595d8a967aa72382ac2f9d39fcc95afd0806" dependencies = [ - "zstd-safe", + "zstd-safe 6.0.5+zstd.1.5.4", +] + +[[package]] +name = "zstd-safe" +version = "5.0.2+zstd.1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d2a5585e04f9eea4b2a3d1eca508c4dee9592a89ef6f450c11719da0726f4db" +dependencies = [ + "libc", + "zstd-sys", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4684a1e4..e2214518 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,7 +72,7 @@ thiserror = "1.0" binascii = "0.1" axum = { version = "0.6.18", features = ["multipart"] } hyper = "0.14.26" -tower-http = { version = "0.4.0", features = ["cors"] } +tower-http = { version = "0.4.0", features = ["cors", "compression-full"] } email_address = "0.2.4" hex = "0.4.3" uuid = { version = "1.3", features = ["v4"] } diff --git a/src/web/api/v1/routes.rs b/src/web/api/v1/routes.rs index 099a1921..44098f4c 100644 --- a/src/web/api/v1/routes.rs +++ b/src/web/api/v1/routes.rs @@ -5,6 +5,7 @@ use std::sync::Arc; use axum::extract::DefaultBodyLimit; use axum::routing::get; use axum::Router; +use tower_http::compression::CompressionLayer; use tower_http::cors::CorsLayer; use super::contexts::about::handlers::about_page_handler; @@ -42,5 +43,5 @@ pub fn router(app_data: Arc) -> Router { router }; - router.layer(DefaultBodyLimit::max(10_485_760)) + router.layer(DefaultBodyLimit::max(10_485_760)).layer(CompressionLayer::new()) }