Crates.io | static-serve |
lib.rs | static-serve |
version | 0.3.0 |
created_at | 2025-03-31 14:26:00.817655+00 |
updated_at | 2025-08-22 14:46:25.132124+00 |
description | A helper for compressing and embedding static assets in an Axum webserver |
homepage | |
repository | https://github.com/M4SS-Code/static-serve |
max_upload_size | |
id | 1613477 |
size | 81,336 |
A Rust library for compressing and embedding static assets in a web server using Axum. This crate provides efficient asset embedding with optional compression (gzip
and zstd
) and conditional requests support.
gzip
and zstd
Add the following to your Cargo.toml
:
[dependencies]
static-serve = "0.2"
axum = "0.8"
Use the embed_assets!
macro to create a static_router()
function in scope which will include your static files, embedding them into your binary:
use static_serve::embed_assets;
embed_assets!("assets", compress = true);
let router = static_router();
This will:
assets
directorygzip
and zstd
(if beneficial)static_router()
function to serve these assetsThe crate automatically handles:
Accept-Encoding
header to serve compressed versions if availableIf-None-Match
header for ETag validation, returning 304 Not Modified
if unchangedpath_to_dir
- a valid &str
string literal of the path to the static files to be includedcompress = false
- compress static files with zstd and gzip, true or false (defaults to false)ignore_dirs = [my_ignore_dir, other_ignore_dir]
- a bracketed list of &str
s of the paths/subdirectories inside the target directory, which should be ignored and not included. (If this parameter is missing, no subdirectories will be ignored)use axum::{Router, Server};
use static_serve::embed_assets;
embed_assets!("public", compress = true);
#[tokio::main]
async fn main() {
let router = static_router();
Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(router.into_make_service())
.await
.unwrap();
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.