mini-static 0.14.5

A secure, async static file server with streaming, traversal protection, and connection limits.
Documentation
use mini_static::Server;
use std::env;
use std::path::Path;
use std::time::Duration;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let port = env::var("PORT")
        .unwrap_or_else(|_| "8080".to_string())
        .parse::<u16>()?;

    let root = env::var("ROOT").unwrap_or_else(|_| "./public".to_string());
    let root_path = Path::new(&root);

    // CSS/JS minification (see `Server::with_minify`) is always on here to demonstrate
    // it — check the served size of `style.css`/`app.js` against the files on disk.
    //
    // `with_immutable_assets` opts fingerprinted filenames (`vendor.a1b2c3.js`) into a
    // year-long, cacheable-forever `Cache-Control`, since a content change would produce
    // a new filename rather than mutating this one. Everything else keeps the default
    // `no-cache` (see `Server::with_immutable_assets`).
    let server = Server::new(root_path)?.with_minify().with_immutable_assets(|path| {
        path.file_name()
            .and_then(|name| name.to_str())
            .is_some_and(|name| name.contains(".a1b2c3."))
    });

    // CSS bundling demo (see `Server::with_css_bundle`): bundles all CSS files from
    // `css-src/` into a single minified output file at `public/style-bundle.css`.
    // When live-reload is enabled, this automatically rebundles whenever any CSS
    // file in the source directory changes.
    let css_src = Path::new("./css-src");
    let css_output = root_path.join("style-bundle.css");

    let mut server = server;

    // CSS bundling demo (see `Server::with_css_bundle`): bundles all CSS files from
    // `css-src/` into a single minified output file at `public/style-bundle.css`.
    let has_css_bundle = if css_src.exists() {
        match server.clone().with_css_bundle(css_src, &css_output) {
            Ok(s) => {
                server = s;
                true
            }
            Err(e) => {
                eprintln!("warning: CSS bundling configuration failed: {}", e);
                false
            }
        }
    } else {
        false
    };

    // Live-reload (background file watcher, SSE stream, injected reload script — see
    // `Server::with_live_reload`) is only enabled in debug builds, matching the
    // convention `mini-unified`'s `add_reload_route` uses: a release build never pays
    // for the watcher or ships the injected script.
    #[cfg(debug_assertions)]
    {
        server = server.with_live_reload();
    }

    // Bind to all interfaces (0.0.0.0) so the server is accessible from outside
    // (e.g., from the host when running in Docker)
    let (_port, handle) = server.run_all(port, Duration::from_secs(30)).await?;

    println!("mini-static listening on 0.0.0.0:{}", port);
    println!("serving files from: {}", root);
    println!("css/js minification enabled");
    println!("immutable caching enabled for *.a1b2c3.* (see vendor.a1b2c3.js)");
    println!("precompressed sidecar demo: bundle.js / bundle.js.gz");
    if has_css_bundle {
        println!("css bundling enabled: {} -> {}", css_src.display(), css_output.display());
    }
    #[cfg(debug_assertions)]
    println!("live-reload enabled at {}", mini_static::LIVE_RELOAD_PATH);

    // Keep the server running until interrupted
    tokio::signal::ctrl_c().await?;
    println!("shutting down...");
    handle.shutdown().await;

    Ok(())
}