Skip to main content

Crate tower_serve_embedded

Crate tower_serve_embedded 

Source
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 the Route table that maps served URLs to them.
EmbeddedFile
A single embedded asset: its content plus the metadata needed to serve it.
Resolved
The outcome of resolving a request path against an Assets set: which file to serve and the Cache-Control to send with it. Returned by Assets::resolve.
Route
A served URL mapped to the file that answers it and the Cache-Control to send.
ServeEmbedded
A tower::Service that serves a set of Assets.

Constants§

IMMUTABLE_CACHE_CONTROL
The Cache-Control value sent for content-hashed URLs and for assets in an immutable_dir: a one-year, public, immutable cache. The bytes behind such a URL never change.