summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 3ee8e21dbe63b2c2a1bcc557d55d8dad834d2d71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// CSRF protection
// https://github.com/heartsucker/iron-csrf
extern crate iron;
#[macro_use]
extern crate log;
#[macro_use]
extern crate router;
extern crate env_logger;
#[macro_use]
extern crate horrorshow;
extern crate systemhttp;
extern crate iron_sessionstorage;
extern crate staticfile;

use iron_sessionstorage::traits::*;
use iron_sessionstorage::SessionStorage;
use iron_sessionstorage::backends::SignedCookieBackend;
use iron::status;
use iron::modifiers::Header;
use iron::headers::ContentType;
use iron::{Iron, Request, IronResult, Response, Chain};
use router::Router;
use horrorshow::prelude::*;
use horrorshow::Raw;
use systemhttp::systemd::unit;
use staticfile::Static;

struct Aaa(String);

impl iron_sessionstorage::Value for Aaa {
    fn get_key() -> &'static str {
        "aaa"
    }
    fn into_raw(self) -> String {
        self.0
    }
    fn from_raw(v: String) -> Option<Self> {
        Some(Aaa(v))
    }
}

fn render_message(message: &str, units: &[unit::Unit]) -> String {
    (html!{
        : Raw("<!DOCTYPE html>");
        html {
            head {
                title: "Title";
                link(rel="stylesheet", type="text/css", href="static/main.css");
            }
            body {
                p {
                    : message
                }
                h1 {
                    : "Units"
                }
                table {
                    tr {
                        th {
                           : "Unit" 
                        }
                        th {
                           : "Active"
                        }
                    }
                    @ for unit in units {
                        tr {
                            td {
                                : &unit.name
                            }
                            td {
                                : format_args!("{} ({})",
                                               &unit.active_state,
                                               &unit.sub_state)
                            }
                        }
                    }
                }
            }
        }
    })
            .into_string()
            .unwrap()
}

fn hello(r: &mut Request) -> IronResult<Response> {
    let mut session_value = match try!(r.session().get::<Aaa>()) {
        Some(aaa) => aaa,
        None => Aaa("".to_owned()),
    };

    let name = r.extensions
        .get::<Router>()
        .unwrap()
        .find("name")
        .unwrap_or("World").to_owned();

    let units = unit::get_units().unwrap();

    let res = Ok(Response::with((status::Ok,
                                 Header(ContentType::html()),
                                 render_message(&format!("Hello, {} ({})",
                                                         name,
                                                         session_value.0),
                                                &units))));

    info!("Updating session value. Current value: {}", session_value.0);
    session_value.0.push('a');
    try!(r.session().set(session_value));
    res
}

fn main() {

    env_logger::init().unwrap();
    let secret = b"secret2".to_vec();
    let router = router!(
        root: get "/" => hello,
        name: get "/:name" => hello,
        css: get "/static/main.css" => Static::new(""),
    );
    let mut chain = Chain::new(router);
    chain.link_around(SessionStorage::new(SignedCookieBackend::new(secret)));
    let _server = Iron::new(chain).http(":::8080").unwrap();
    println!("On 8080");
}