1 unstable release

0.1.0 Dec 17, 2023

#4 in #serve-file

Download history 1137/week @ 2024-07-22 1326/week @ 2024-07-29 1063/week @ 2024-08-05 1412/week @ 2024-08-12 1331/week @ 2024-08-19 1040/week @ 2024-08-26 1067/week @ 2024-09-02 960/week @ 2024-09-09 1382/week @ 2024-09-16 1717/week @ 2024-09-23 1019/week @ 2024-09-30 1126/week @ 2024-10-07 2004/week @ 2024-10-14 1449/week @ 2024-10-21 2176/week @ 2024-10-28 1173/week @ 2024-11-04

6,826 downloads per month

MIT license

2.5MB
930 lines

axum_embed is a library that provides a service for serving embedded files using the axum web framework.

This library uses the rust_embed crate to embedded files into the binary at compile time, and the axum crate to serve these files over HTTP.

Features

  • Serve embedded files over HTTP
  • Customizable 404, fallback, and index files
  • Response compressed files if the client supports it and the compressed file exists
  • Response 304 if the client has the same file (based on ETag)
  • Redirect to the directory if the client requests a directory without a trailing slash

Example

use rust_embed::RustEmbed;
use axum_embed::ServeEmbed;
use tokio::net::TcpListener;

#[derive(RustEmbed, Clone)]
#[folder = "examples/assets/"]
struct Assets;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let listener = TcpListener::bind("127.0.0.1:8080").await?;
    let serve_assets = ServeEmbed::<Assets>::new();
    let app = axum::Router::new().nest_service("/", serve_assets);
    axum::serve(listener, app).await?;
    Ok(())
}

Usage

Please see the examples directory for a working example.

Serve compressed file

The axum_embed library has the capability to serve compressed files, given that the client supports it and the compressed file is available. The compression methods supported include br (Brotli), gzip, and deflate. If the client supports multiple compression methods, axum_embed will select the first one listed in the Accept-Encoding header. Please note that the weight of encoding is not considered in this ction. In the absence of client support for any compression methods, axum_embed will serve the file in its uncompressed form. If a file with the extension .br (for Brotli), .gz (for GZip), or .zz (for Deflate) is available, axum_embed will serve the file in its compressed form. An uncompressed file is must be available for the compressed file to be served.

Dependencies

~4–11MB
~115K SLoC