Skip to main content

Crate tower_embed

Crate tower_embed 

Source
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-Type header generation based on file MIME type guessed from extension.
  • ETag header generation and validation.
  • Last-Modified header 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.

The optional feature astro can be used to enable support for embedding Astro project. In release mode the project is build and embedded as static assets, while in debug mode the service acts as a proxy server pointing to the development server.

§Features

  • astro - Enable support for Astro projects.

§Usage

use axum::Router;
use tower_embed::{EmbedExt, EmbedFolder, ServeEmbed};

#[derive(Embed)]
#[embed(folder = "assets")]
struct Assets;

#[tokio::main]
async fn main() {
    let assets = ServeEmbed::<Assets>::new().with_not_found(Assets::not_found_page("404.html"));
    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.

Structs§

ServeEmbed
Service that serves files from embedded assets.

Traits§

Embed
The trait used to access to embedded assets.
EmbedExt
Extension trait for Embed.
EmbedFolder
A trait for types that embed a folder of assets.

Derive Macros§

Embed
Derive the Embed trait for unit struct, embedding assets from a folder.