aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/mt-webface.rs
blob: 0940ea0a4d23ad3f5297be40b5063a6a443f0745 (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

extern crate pencil;
#[macro_use] extern crate log;
extern crate env_logger;

use std::collections::BTreeMap;
use pencil::Pencil;
use pencil::{Request, PencilResult, Response, HTTPError};


fn home(r: &mut Request) -> PencilResult {
    let context: BTreeMap<String,String> = BTreeMap::new();
    return r.app.render_template("home.html", &context);
}

fn readme(r: &mut Request) -> PencilResult {
    let mut context = BTreeMap::new();
    let raw_text = include_str!("../../README.txt");
    context.insert("raw_text".to_string(), raw_text.to_string());
    return r.app.render_template("raw.html", &context);
}

fn model_list(_: &mut Request) -> PencilResult {
    Ok(Response::from("List of Models"))
}

fn model_view(r: &mut Request) -> PencilResult {
    let model_id = r.view_args.get("model_id").unwrap();
    let mut context = BTreeMap::new();
    context.insert("model_id".to_string(), model_id.to_string());
    return r.app.render_template("model_view.html", &context);
}

fn page_not_found(_: HTTPError) -> PencilResult {
    let mut response = Response::from("404: Not Found");
    response.status_code = 500;
    Ok(response)
}

fn server_error(_: HTTPError) -> PencilResult {
    let mut response = Response::from("500: Server Error");
    response.status_code = 500;
    Ok(response)
}

fn main() {

    env_logger::init().unwrap();

    let mut app = Pencil::new("webface");
    app.name = "modelthing-webface".to_string();
    app.set_debug(true);
    app.set_log_level();
    app.httperrorhandler(404, page_not_found);
    app.httperrorhandler(500, server_error);
    app.enable_static_file_handling();
    debug!("root_path: {}", app.root_path);

    app.register_template("base.html");
    app.register_template("home.html");
    app.register_template("raw.html");
    app.get("/", "home", home);
    app.get("/readme/", "readme", readme);
    app.get("/model_list/", "model_list", model_list);
    app.register_template("model_view.html");
    app.get("/model/<model_id:string>", "model", model_view);

    info!("Running on http://localhost:5000/");
    app.run("127.0.0.1:5000");
}