Code Monkey home page Code Monkey logo

axum-embed's Introduction

axum-embed

Crates.io Crates.io Build docs.rs GitHub code size in bytes

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.

axum-embed's People

Contributors

informationsea avatar jmkng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.