1
0
mirror of https://github.com/actix/examples synced 2024-12-02 18:02:22 +01:00
examples/template_engines/askama/src/main.rs
2021-11-29 14:46:19 +00:00

46 lines
1.0 KiB
Rust

use std::collections::HashMap;
use actix_web::{middleware, web, App, HttpResponse, HttpServer, Result};
use askama::Template;
#[derive(Template)]
#[template(path = "user.html")]
struct UserTemplate<'a> {
name: &'a str,
text: &'a str,
}
#[derive(Template)]
#[template(path = "index.html")]
struct Index;
async fn index(query: web::Query<HashMap<String, String>>) -> Result<HttpResponse> {
let s = if let Some(name) = query.get("name") {
UserTemplate {
name,
text: "Welcome!",
}
.render()
.unwrap()
} else {
Index.render().unwrap()
};
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
// start http server
HttpServer::new(move || {
App::new()
.wrap(middleware::Logger::default())
.service(web::resource("/").route(web::get().to(index)))
})
.bind("127.0.0.1:8080")?
.run()
.await
}