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