Expand description
Embed content-hashed static web assets into your binary and serve them with tower.
tower-serve-embedded is similar to rust-embed but tailored
for serving web assets from a tower/axum stack: ordinary files are embedded and exposed at
a content-hashed URL that mirrors their location in your crate
(assets/css/style.css → /assets/css/style.9f3a1c2b.css), so they can be served immutable
with a one-year cache and still update instantly when their content changes.
§How it works
The heavy lifting (walking the directory, hashing, MIME detection, codegen) happens at build
time in tower-serve-embedded-build, called
from your build.rs. There are no proc macros — the generated code is plain data plus a
tiny macro_rules!, so IDE support stays excellent.
// build.rs
fn main() {
tower_serve_embedded_build::Builder::new("assets").emit().unwrap();
}// src/main.rs
tower_serve_embedded::embed!(); // pulls in `ASSETS` and the `asset!` macro
// Reference assets by their path relative to the crate root. Resolved at compile time —
// typos are compile errors:
// link rel="stylesheet" href=(asset!("assets/css/style.css"))
// => "/assets/css/style.9f3a1c2b.css"
// Serve them: generated asset URLs are already full paths, so mount as a fallback.
// Router::new().fallback_service(ASSETS.service())See examples/ in the repository for complete, runnable setups (axum, actix, warp).
Macros§
- embed
- Pull in the assets generated by
tower-serve-embedded-build.
Structs§
- Assets
- A collection of
EmbeddedFiles plus theRoutetable that maps served URLs to them. - Embedded
File - A single embedded asset: its content plus the metadata needed to serve it.
- Resolved
- The outcome of resolving a request path against an
Assetsset: which file to serve and theCache-Controlto send with it. Returned byAssets::resolve. - Route
- A served URL mapped to the file that answers it and the
Cache-Controlto send. - Serve
Embedded - A
tower::Servicethat serves a set ofAssets.
Constants§
- IMMUTABLE_
CACHE_ CONTROL - The
Cache-Controlvalue sent for content-hashed URLs and for assets in animmutable_dir: a one-year,public,immutablecache. The bytes behind such a URL never change.