axum_static

Crates.ioaxum_static
lib.rsaxum_static
version1.7.1
sourcesrc
created_at2023-01-22 04:47:36.194269
updated_at2024-04-29 12:46:46.042078
descriptionstatic file serving router for axum server
homepagehttps://github.com/myyrakle/axum_static/blob/master/README.md
repositoryhttps://github.com/myyrakle/axum_static
max_upload_size
id764756
size7,489
myyrakle (myyrakle)

documentation

https://github.com/myyrakle/axum_static/blob/master/README.md

README

axum_static

GitHub license

static file serving for axum

Version

You must use axum_static that matches your axum version.

  • axum 0.6 => axum_static ~1.6.*
  • axum 0.7 => axum_static ~1.7.*

Usage

First install crate.

cargo add axum_static

Then, create a static route and nest it in the existing route like so

let app = Router::new()
        .nest("/", axum_static::static_router("public"))

If your app has state, you'll need to add with_state, because static_router does not use state (()):

let app = Router::new()
        .route("/", get(index))
        ......
        .nest("/static", axum_static::static_router("static").with_state())
        ......
        .with_state(YourAppState { ... })

The argument of the static_router function is the path to read static files based on the project root path.

Then you can read the file like this. It can also be a sub directory.

This is the end.

Commit count: 22

cargo fmt