Expand description
This crate provides a tower service designed to provide embedded static
assets support for web application. This service includes the following HTTP features:
- Support for GET and HEAD requests
Content-Typeheader generation based on file MIME type guessed from extension.ETagheader generation and validation.Last-Modifiedheader generation and validation.- Customizable 404 page.
In debug mode, assets are served directly from the filesystem to facilitate rapid
development. Both ETag and Last-Modified headers are not generated in this mode.
§Usage
use axum::Router;
use tower_embed::{Embed, EmbedExt, ServeEmbed};
#[derive(Embed)]
#[embed(folder = "assets")]
struct Assets;
#[tokio::main]
async fn main() {
let assets = ServeEmbed::builder()
.not_found_service(Assets::not_found_page("404.html"))
.build::<Assets>();
let router = Router::new().fallback_service(assets);
let listener = tokio::net::TcpListener::bind("127.0.0.1:8080")
.await
.unwrap();
axum::serve::serve(listener, router).await.unwrap();
}Please see the examples directory for working examples.
Modules§
- core
- Core functionalities of tower-embed.
Structs§
- Body
- The body used in responses.
- NotFound
Page - A service that serves a custom not found page.
- Response
Future - Response future of
ServeEmbed - Serve
Embed - Service that serves files from embedded assets.
- Serve
Embed Builder - Builder for
ServeEmbedservice.
Traits§
Derive Macros§
- Embed
- Derive the
Embedtrait for unit struct, embedding assets from a folder.