1
0
mirror of https://github.com/actix/examples synced 2024-12-04 10:41:56 +01:00
examples/template_askama/src/main.rs

39 lines
887 B
Rust
Raw Normal View History

2018-05-08 20:08:43 +02:00
use std::collections::HashMap;
2019-03-10 07:38:15 +01:00
use actix_web::{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-03-10 07:38:15 +01:00
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 {
name: name,
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))
}
2019-03-10 07:38:15 +01:00
fn main() -> std::io::Result<()> {
2018-04-24 18:12:48 +02:00
// start http server
2019-03-10 07:38:15 +01:00
HttpServer::new(move || {
App::new().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")?
.run()
2018-04-24 18:12:48 +02:00
}