1.2.1 |
|
---|---|
1.2.0 |
|
1.1.3 |
|
1.1.2 |
|
1.0.8 |
|
#30 in #serve-static
每月24次下载
用于 simpleshortener
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();
}