diff options
author | zuisong <com.me@foxmail.com> | 2024-01-14 14:55:24 +0000 |
---|---|---|
committer | zuisong <com.me@foxmail.com> | 2024-01-16 13:02:29 +0000 |
commit | b5e2d58f5e0a32e773e6b6b94624c8e3863179b5 (patch) | |
tree | 491e9891bb31a49a2954de819b4620f03ce57740 /src | |
parent | Compress response using `actix_web::middleware::compress` (diff) | |
download | miniserve-b5e2d58f5e0a32e773e6b6b94624c8e3863179b5.tar.gz miniserve-b5e2d58f5e0a32e773e6b6b94624c8e3863179b5.zip |
Add option for compress response
Diffstat (limited to 'src')
-rw-r--r-- | src/args.rs | 12 | ||||
-rw-r--r-- | src/config.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 5 |
3 files changed, 20 insertions, 1 deletions
diff --git a/src/args.rs b/src/args.rs index cc8b0f1..25f2915 100644 --- a/src/args.rs +++ b/src/args.rs @@ -214,6 +214,18 @@ pub struct CliArgs { #[arg(short = 'z', long = "enable-zip", env = "MINISERVE_ENABLE_ZIP")] pub enable_zip: bool, + /// Enable compress response + /// + /// WARNING: Enabling this option may slow down transfers due to CPU overhead, so it is disabled by default. + /// + /// Only enable this option if you know that your users have slow connections or if you want to minimize your server's bandwidth usage. + #[arg( + short = 'C', + long = "compress-response", + env = "MINISERVE_COMPRESS_RESPONSE" + )] + pub compress_response: bool, + /// List directories first #[arg(short = 'D', long = "dirs-first", env = "MINISERVE_DIRS_FIRST")] pub dirs_first: bool, diff --git a/src/config.rs b/src/config.rs index bfe916b..6e4a1eb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -119,6 +119,9 @@ pub struct MiniserveConfig { /// If false, creation of zip archives is disabled pub zip_enabled: bool, + /// Enable compress response + pub compress_response: bool, + /// If enabled, directories are listed first pub dirs_first: bool, @@ -309,6 +312,7 @@ impl MiniserveConfig { show_wget_footer: args.show_wget_footer, readme: args.readme, tls_rustls_config: tls_rustls_server_config, + compress_response: args.compress_response, }) } } diff --git a/src/main.rs b/src/main.rs index cdded78..ce21260 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,7 +202,10 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> { .app_data(stylesheet.clone()) .wrap_fn(errors::error_page_middleware) .wrap(middleware::Logger::default()) - .wrap(middleware::Compress::default()) + .wrap(middleware::Condition::new( + miniserve_config.compress_response, + middleware::Compress::default(), + )) .route(&inside_config.favicon_route, web::get().to(favicon)) .route(&inside_config.css_route, web::get().to(css)) .service( |