#axum #static-file #macro #serve-static

已删除 axum_static_macro

用于从Axum的二进制文件中服务静态文件并为其提供内容类型列表的宏

1.2.1 2022年10月4日
1.2.0 2022年5月29日
1.1.3 2022年4月14日
1.1.2 2022年4月13日
1.0.8 2022年4月3日

#30 in #serve-static

每月24次下载
用于 simpleshortener

MIT/Apache

6KB

Axum静态宏

! ! ! 此仓库已不再维护! ! ! !

相反,您可以使用

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

此包包含一个宏(static_file),它接受名称、文件路径和内容类型的参数,并填充适当的静态内容。
在调试模式下,它将实时读取文件,因此您可以在不重新编译程序的情况下更改它。(仅在crate根目录中有效。)
它接受三个参数。函数名称(这是您在axum get处理程序中包装的内容)、文件路径和内容类型。
发布模式下不会panic。调试模式下如果文件不存在可能会panic。
包含一个模块(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();
}

无运行时依赖