summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 535966144e260e42d797aa8a021a4a8415deb6de (plain) (blame)
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
use handlebars::Handlebars;
use serde::Serialize;
use std::sync::Arc;
use warp::{http::Uri, Filter, Rejection};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Build template registry
    let mut hbs = Handlebars::new();
    hbs.register_template_file("base", "templates/base.hbs")?;
    hbs.register_template_file("index", "templates/index.hbs")?;
    hbs.register_template_file("policies", "templates/policies.hbs")?;
    hbs.register_template_file("about", "templates/about.hbs")?;
    let hbs = Arc::new(hbs);

    // Build routes
    let routes = index_filter(hbs.clone())
        .or(template_filter(hbs.clone()))
        .or(redirect_static_ext())
        .or(evals_list())
        .or(warp::fs::dir("static"));

    // Start server
    warp::serve(routes).run(([127, 0, 0, 1], 8080)).await;

    // Done
    Ok(())
}

fn index_filter(
    hbs: Arc<Handlebars<'static>>,
) -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone {
    warp::get()
        .and(warp::path::end())
        .map(move || render("index", hbs.clone(), ""))
}

fn template_filter(
    hbs: Arc<Handlebars<'static>>,
) -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone {
    warp::path!(String).and_then(move |name: String| {
        let hbs = hbs.clone();
        async move {
            if hbs.has_template(&name) {
                Ok(render(&name, hbs, ""))
            } else {
                Err(warp::reject::not_found())
            }
        }
    })
}

fn redirect_static_ext() -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone {
    warp::path!(String).and_then(move |path: String| async move {
        if let Some(prefix) = path.strip_suffix(".php") {
            return Ok(warp::redirect(
                prefix
                    .parse::<Uri>()
                    .unwrap_or_else(|_| Uri::from_static("/")),
            ));
        }

        if let Some(prefix) = path.strip_suffix(".html") {
            return Ok(warp::redirect(
                prefix
                    .parse::<Uri>()
                    .unwrap_or_else(|_| Uri::from_static("/")),
            ));
        }

        Err(warp::reject::not_found())
    })
}

fn evals_list() -> impl Filter<Extract = (&'static str,), Error = Rejection> + Clone {
    warp::get()
        .and(warp::path("evals"))
        .and(warp::path::end())
        .map(|| "evals here")
}

fn render<T>(template: &str, hbs: Arc<Handlebars>, value: T) -> impl warp::Reply
where
    T: Serialize,
{
    let render = hbs
        .render(template, &value)
        .unwrap_or_else(|err| err.to_string());
    warp::reply::html(render)
}