2018-04-24 18:12:48 +02:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate askama;
|
|
|
|
|
2018-05-08 20:08:43 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use actix_web::{http, server, App, HttpResponse, Query, 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;
|
|
|
|
|
2018-05-08 20:08:43 +02:00
|
|
|
fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> {
|
|
|
|
let s = if let Some(name) = query.get("name") {
|
2018-04-24 18:12:48 +02:00
|
|
|
UserTemplate {
|
|
|
|
name: name,
|
|
|
|
text: "Welcome!",
|
|
|
|
}.render()
|
|
|
|
.unwrap()
|
|
|
|
} else {
|
|
|
|
Index.render().unwrap()
|
|
|
|
};
|
|
|
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-05-08 20:08:43 +02:00
|
|
|
let sys = actix::System::new("template-askama");
|
2018-04-24 18:12:48 +02:00
|
|
|
|
|
|
|
// start http server
|
2018-05-08 20:08:43 +02:00
|
|
|
server::new(move || {
|
|
|
|
App::new().resource("/", |r| r.method(http::Method::GET).with(index))
|
|
|
|
}).bind("0.0.0.0:8080")
|
2018-04-24 18:12:48 +02:00
|
|
|
.unwrap()
|
|
|
|
.start();
|
|
|
|
|
|
|
|
println!("Started http server: 127.0.0.1:8080");
|
|
|
|
let _ = sys.run();
|
|
|
|
}
|