From 5d00d628b1298316c4e0aeb1bef27a71a3ea3c4a Mon Sep 17 00:00:00 2001 From: Husni Zuhdi Date: Mon, 23 Dec 2024 12:32:12 +0700 Subject: [PATCH] feat: add compression layer from axum/tower middleware --- Cargo.lock | 51 ++++++++++++++++++++++++++++++++++++++++++++- internal/Cargo.toml | 2 +- internal/src/app.rs | 3 +++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d630c0..a6bdef8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,12 @@ dependencies = [ "gimli", ] +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "adler2" version = "2.0.0" @@ -164,6 +170,19 @@ dependencies = [ "nom", ] +[[package]] +name = "async-compression" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df895a515f70646414f4b45c0b79082783b80552b373a68283012928df56f522" +dependencies = [ + "flate2", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", +] + [[package]] name = "async-trait" version = "0.1.83" @@ -266,7 +285,7 @@ dependencies = [ "addr2line", "cfg-if", "libc", - "miniz_oxide", + "miniz_oxide 0.8.2", "object", "rustc-demangle", "windows-targets 0.52.6", @@ -432,6 +451,15 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5" +[[package]] +name = "crc32fast" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3855a8a784b474f333699ef2bbca9db2c4a1f6d9088a90a2d25b1eb53111eaa" +dependencies = [ + "cfg-if", +] + [[package]] name = "crossbeam-queue" version = "0.3.12" @@ -586,6 +614,16 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" +[[package]] +name = "flate2" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae" +dependencies = [ + "crc32fast", + "miniz_oxide 0.7.4", +] + [[package]] name = "flume" version = "0.11.1" @@ -1303,6 +1341,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "miniz_oxide" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +dependencies = [ + "adler", +] + [[package]] name = "miniz_oxide" version = "0.8.2" @@ -2553,8 +2600,10 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5" dependencies = [ + "async-compression", "bitflags", "bytes", + "futures-core", "futures-util", "http", "http-body", diff --git a/internal/Cargo.toml b/internal/Cargo.toml index e72fd42..1e41ea7 100644 --- a/internal/Cargo.toml +++ b/internal/Cargo.toml @@ -12,7 +12,7 @@ tokio = { version = "1.0", features = ["full"] } tracing = { version = "0.1", features = ["attributes"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] } tower = { version = "0.4", features = ["util"] } -tower-http = { version = "0.5.0", features = ["fs", "trace"] } +tower-http = { version = "0.5.0", features = ["fs", "trace", "compression-gzip"] } askama = { version = "0.12.1"} serde = { version="1.0.192", features = ["derive"]} serde_json = "1.0.108" diff --git a/internal/src/app.rs b/internal/src/app.rs index e8bb17f..7d473e5 100644 --- a/internal/src/app.rs +++ b/internal/src/app.rs @@ -5,6 +5,8 @@ use axum::{ routing::{get, get_service}, Router, }; +use tower::ServiceBuilder; +use tower_http::compression::CompressionLayer; use tower_http::services::{ServeDir, ServeFile}; use tracing::info; @@ -37,6 +39,7 @@ pub async fn app() -> () { get_service(ServeFile::new("./statics/styles.css")), ) .with_state(app_state) + .layer(ServiceBuilder::new().layer(CompressionLayer::new())) .fallback(get(handler::status::get_404_not_found)); // Start Axum Application