压缩响应

Response 内容压缩处理的中间件.

提供对三种压缩格式的支持: br, gzip, deflate. 可以根据需求配置各个压缩方式的优先度等.

HTTP 协议中的数据压缩

  1. 文件格式层面:特定文件类型使用专门的压缩算法
  2. HTTP 协议层面:通过内容编码实现端到端的资源压缩传输
  3. 连接层面:在 HTTP 连接的节点之间进行数据压缩

更多关于HTTP压缩的信息可以参考 Mozilla 开发者网络的文档

示例代码

main.rs
Cargo.toml
use salvo::prelude::*;

#[tokio::main]
async fn main() {
    // Initialize logging system
    tracing_subscriber::fmt().init();

    // Print current working directory for debugging
    println!("current_dir: {:?}", std::env::current_dir().unwrap());

    // Set up base directory for static files
    let current_dir = std::env::current_dir().unwrap();
    let base_dir = if !current_dir.to_str().unwrap().ends_with("compression") {
        current_dir
            .join("compression/static")
            .canonicalize()
            .unwrap()
    } else {
        current_dir.join("static").canonicalize().unwrap()
    };
    println!("Base Dir: {base_dir:?}");

    // Configure router with different compression settings for different paths
    let router = Router::new()
        // WebSocket chat with forced compression priority
        .push(
            Router::with_hoop(Compression::new().force_priority(true))
                .path("ws_chat")
                .get(StaticFile::new(base_dir.join("ws_chat.txt"))),
        )
        // SSE chat with Brotli compression
        .push(
            Router::with_hoop(Compression::new().enable_brotli(CompressionLevel::Fastest))
                .path("sse_chat")
                .get(StaticFile::new(base_dir.join("sse_chat.txt"))),
        )
        // Todos with Zstd compression
        .push(
            Router::with_hoop(Compression::new().enable_zstd(CompressionLevel::Fastest))
                .path("todos")
                .get(StaticFile::new(base_dir.join("todos.txt"))),
        )
        // All other paths with Gzip compression
        .push(
            Router::with_hoop(Compression::new().enable_gzip(CompressionLevel::Fastest))
                .path("{*path}")
                .get(StaticDir::new(base_dir)),
        );

    // Bind server to port 8698 and start serving
    let acceptor = TcpListener::new("0.0.0.0:8698").bind().await;
    Server::new(acceptor).serve(router).await;
}