【Rust】Actixでaskama(template)の使い方

axumの方が優勢な気がしますが、actixでも問題なくルーティングとテンプレートを使った開発ができそうです。

use actix_web::{get, web, App, HttpServer, HttpResponse};
use askama::Template;
use askama_actix::TemplateToResponse;

#[derive(Template)]
#[template(path = "hello.html")]
struct HelloTemplate {
    name: String,
}

#[derive(Template)]
#[template(path = "todo.html")]
struct TodoTemplate {
    tasks: Vec<String>,
}

#[get("/hello/{name}")]
async fn hello(name: web::Path<String>) -> HttpResponse {
    let hello = HelloTemplate {
        name: name.into_inner(),
    };
    hello.to_response()
}

#[get("/")]
async fn todo() -> HttpResponse {
    let tasks = vec!["task1".to_string(), "task2".to_string(), "task3".to_string()];
    let todo = TodoTemplate { tasks };
    todo.to_response()
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(hello).service(todo))
        .bind(("0.0.0.0", 8080))?
        .run()
        .await
}
<html>
<head>
    <title>ToDo</title>
</head>
<body>
    <ul>
        {% for task in tasks %}
        <li>{{ task }}</li>
        {% endfor %}
    </ul>
</body>
</html>