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 || {
|
2019-04-01 10:47:15 +02:00
|
|
|
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
|
|
|
}
|