1
0
mirror of https://github.com/actix/examples synced 2024-12-03 18:22:14 +01:00
examples/template_engines/askama/src/main.rs

46 lines
1.0 KiB
Rust
Raw Normal View History

2018-05-08 20:08:43 +02:00
use std::collections::HashMap;
use actix_web::{middleware, web, App, HttpResponse, HttpServer, Result};
2018-04-24 18:12:48 +02:00
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;
2019-12-07 18:59:24 +01:00
async fn index(query: web::Query<HashMap<String, String>>) -> Result<HttpResponse> {
2018-05-08 20:08:43 +02:00
let s = if let Some(name) = query.get("name") {
2018-04-24 18:12:48 +02:00
UserTemplate {
2019-09-04 17:04:57 +02:00
name,
2018-04-24 18:12:48 +02:00
text: "Welcome!",
2019-03-10 03:03:09 +01:00
}
.render()
.unwrap()
2018-04-24 18:12:48 +02:00
} else {
Index.render().unwrap()
};
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
2020-09-12 17:49:45 +02:00
#[actix_web::main]
2019-12-07 18:59:24 +01:00
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
2018-04-24 18:12:48 +02:00
// start http server
2019-03-10 07:38:15 +01:00
HttpServer::new(move || {
App::new()
.wrap(middleware::Logger::default())
.service(web::resource("/").route(web::get().to(index)))
2019-03-10 03:03:09 +01:00
})
2019-03-10 07:38:15 +01:00
.bind("127.0.0.1:8080")?
2019-12-25 17:48:33 +01:00
.run()
2019-12-07 18:59:24 +01:00
.await
2018-04-24 18:12:48 +02:00
}