Initial commit
[rup.git] / src / main.rs
1 extern crate actix_web;
2 extern crate askama;
3
4 use actix_files as fs;
5 use actix_web::{web, middleware, App, HttpServer, HttpResponse, Responder, Result, web::Query};
6 use askama::Template;
7 use serde::{Deserialize};
8
9 #[derive(Template)]
10 #[template(path = "main.html")]
11 struct MainTemplate<'a> {
12 sentence: &'a str,
13 }
14
15 #[derive(Deserialize)]
16 pub struct Request {
17 m: Option<String>
18 }
19
20 fn main_page(query: Query<Request>) -> Result<HttpResponse> {
21 let m =
22 match &query.m {
23 Some(b) => &b,
24 None => "Marc, roule un pet'!"
25 };
26
27 let hello = MainTemplate { sentence: m };
28
29 let s = hello.render().unwrap();
30 Ok(HttpResponse::Ok().content_type("text/html").body(s))
31 }
32
33 fn main() -> std::io::Result<()> {
34 HttpServer::new(
35 || {
36 App::new()
37 .wrap(middleware::Compress::default())
38 .wrap(middleware::Logger::default())
39 .service(web::resource("/").to(main_page))
40 .service(fs::Files::new("/static", "static").show_files_listing())
41 }
42 )
43 .bind("0.0.0.0:8082")
44 .expect("Can not bind to port 8082")
45 .run()
46 }