12 releases (stable)

new 1.7.1 Apr 29, 2024
1.7.0 Apr 24, 2024
1.2.3 Oct 24, 2023
1.2.2 Jul 16, 2023
0.1.2 Jan 22, 2023

#917 in HTTP server

Download history 31/week @ 2024-01-11 77/week @ 2024-01-18 54/week @ 2024-01-25 52/week @ 2024-02-01 53/week @ 2024-02-08 63/week @ 2024-02-15 188/week @ 2024-02-22 201/week @ 2024-02-29 365/week @ 2024-03-07 181/week @ 2024-03-14 151/week @ 2024-03-21 134/week @ 2024-03-28 91/week @ 2024-04-04 116/week @ 2024-04-11 360/week @ 2024-04-18 468/week @ 2024-04-25

1,061 downloads per month

MIT license

8KB
104 lines

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.

Dependencies

~6.5–8.5MB
~153K SLoC