Compression
Middleware for Response
content compression processing.
Provides support for three compression formats: br
, gzip
, deflate
. You can configure the priority of each compression method according to your needs.
Example
use salvo::compression::Compression;
use salvo::prelude::*;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();
let base_dir = std::env::current_exe()
.unwrap()
.join("../../../examples/compression/static")
.canonicalize()
.unwrap();
println!("Base Dir: {:?}", base_dir);
let router = Router::new()
.push(
Router::with_hoop(Compression::new().force_priority(true))
.path("ws_chat")
.get(StaticFile::new(base_dir.join("ws_chat.txt"))),
)
.push(
Router::with_hoop(Compression::new().enable_brotli(CompressionLevel::Fastest))
.path("sse_chat")
.get(StaticFile::new(base_dir.join("sse_chat.txt"))),
)
.push(
Router::with_hoop(Compression::new().enable_deflate(CompressionLevel::Fastest))
.path("todos")
.get(StaticFile::new(base_dir.join("todos.txt"))),
)
.push(
Router::with_hoop(Compression::new().enable_gzip(CompressionLevel::Fastest))
.path("<*path>")
.get(StaticDir::new(base_dir)),
);
let acceptor = TcpListener::new("127.0.0.1:5800").bind().await;
Server::new(acceptor).serve(router).await;
}
[package]
name = "example-compression"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
salvo = { workspace = true, features = ["compression", "serve-static"] }
tokio = { version = "1", features = ["macros"] }
tracing = "0.1"
tracing-subscriber = "0.3"