summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 089382587a0ebce38a1303c3fdffa5e821ca4fbc (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
extern crate iron;
#[macro_use]
extern crate log;
#[macro_use]
extern crate router;
extern crate env_logger;
#[macro_use]
extern crate horrorshow;

use iron::status;
use iron::modifiers::Header;
use iron::headers::ContentType;
use iron::{Iron, Request, IronResult, Response};
use router::Router;

use horrorshow::prelude::*;
use horrorshow::Raw;

use std::process::Command;

fn render_message(message: &str) -> String {
    (html!{
        : Raw("<!DOCTYPE html>");
        html {
            head { title: "Title"; }
            body {
                p {
                    : message
                }
            }
        }
    })
            .into_string()
            .unwrap()
}

fn hello(r: &mut Request) -> IronResult<Response> {
    let name = r.extensions
        .get::<Router>()
        .unwrap()
        .find("name")
        .unwrap_or("World");
    let output = Command::new("date").output().unwrap().stdout;
    let date = String::from_utf8_lossy(&output);

    Ok(Response::with((status::Ok,
                       Header(ContentType::html()),
                       render_message(&format!("Hello, {}. The time is {}", name, date)))))
}

fn main() {
    env_logger::init().unwrap();
    info!("Some debug info");
    let router = router!(
        root: get "/" => hello,
        name: get "/:name" => hello);
    let _server = Iron::new(router).http(":::8080").unwrap();
    println!("On 8080");
}