axum_static_macro

Crates.ioaxum_static_macro
lib.rsaxum_static_macro
version1.2.1
sourcesrc
created_at2022-04-03 17:26:17.035127
updated_at2022-10-04 20:37:55.011577
descriptionA macro to serve static files from the binary in Axum, and a list of content types for them
homepage
repositoryhttps://github.com/randomairborne/axum_static_macro
max_upload_size
id561373
size5,060
valkyrie_pilot (randomairborne)

documentation

README

Axum Static Macro

! ! ! THIS REPOSITORY IS NO LONGER MAINTAINED! ! ! !

Instead, you can use

router.route("/", get(|| async { ([("Content-Type", "text/html")], include_bytes!("index.html")) }))
axum = "0.4" # Required
axum_static_macro = "1"

This package has a single macro (static_file) which takes arguments for name, file path, and content type and fills out the proper statics.
In debug mode, it will read the file live so you can change it without recompiling the program. (Works only in crate root.)
It takes three arguments. Function name (this is what you wrap in the axum get handler), file path, and content type.
Does not panic in release mode. Debug mode can panic if the file does not exist.
Includes a module (content_types) with consts for common content types.

#[tokio::main]
async fn main() {
    // create our static file handler
    axum_static_macro::static_file!(index, "index.html", axum_static_macro::content_types::HTML);
    // build our application with a single route
    let app = axum::Router::new().route("/", axum::routing::get(index));
    // run it with hyper on localhost:3000
    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}
Commit count: 30

cargo fmt