tower-http = { version = “0.6.2”, features = [“fs”] }
staticフォルダにcssファイルを置きます。
static/styles.css
h1 {
color:red;
}
template/test.html
<head>
<title>title</title>
<link rel="stylesheet" href="styles.css">
</head>
<h1>Hello world</h1>
main.rs
use tower_http::services::{ServeDir, ServeFile};
use axum::{
routing::get,
Router,
};
#[tokio::main]
async fn main() {
let serve_dir = ServeDir::new("static").not_found_service(ServeFile::new("static"));
let app = Router::new()
.route("/", get(handle_index))
.nest_service("/static", serve_dir.clone())
.fallback_service(serve_dir);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn handle_index()-> axum::response::Html<String> {
let tera = tera::Tera::new("templates/*").unwrap();
let mut context = tera::Context::new();
context.insert("title", "Index page");
let output = tera.render("test.html", &context);
axum::response::Html(output.unwrap())
}

なるほど、これでCSSもjsも自由にコーディングできますね。