aboutsummaryrefslogtreecommitdiff
path: root/src/controller/utils.rs
blob: 340a5c7be58b5d43b1e5358296c4903c61bcbf0c (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
use http_body_util::Full;
use hyper::body::Bytes;
use hyper::header::{
    HeaderName, HeaderValue, CACHE_CONTROL, CONTENT_TYPE, LOCATION,
};
use hyper::{Response, StatusCode};
use std::collections::HashMap;

use crate::controller::error;

pub fn with_headers(
    response: Response<Full<Bytes>>,
    headers: Vec<(HeaderName, &str)>,
) -> Response<Full<Bytes>> {
    let mut response = response;
    let response_headers = response.headers_mut();
    for (name, value) in headers {
        response_headers.insert(name, HeaderValue::from_str(value).unwrap());
    }
    response
}

pub fn template(
    assets: &HashMap<String, String>,
    templates: &minijinja::Environment<'_>,
    path: &str,
    context: minijinja::Value,
) -> Response<Full<Bytes>> {
    let context = minijinja::context! { ..context, ..minijinja::context! {
        assets => assets
    }};

    match render_template(templates, path, context) {
        Ok(template) => with_headers(
            Response::new(template.into()),
            vec![(CONTENT_TYPE, "text/html"), (CACHE_CONTROL, "no-cache")],
        ),
        Err(err) => {
            log::error!("ERROR template rendering {}\n{:?}", path, err);
            server_error(
                assets,
                templates,
                &format!(
                    "Erreur lors de la préparation de la page : {:?}",
                    err
                ),
            )
        }
    }
}

fn render_template(
    templates: &minijinja::Environment<'_>,
    name: &str,
    context: minijinja::Value,
) -> Result<String, minijinja::Error> {
    let template = templates.get_template(name)?;
    template.render(context)
}

fn server_error(
    assets: &HashMap<String, String>,
    templates: &minijinja::Environment<'_>,
    msg: &str,
) -> Response<Full<Bytes>> {
    with_headers(
        Response::new(
            error::template(assets, templates, "Erreur serveur", msg).into(),
        ),
        vec![(CONTENT_TYPE, "text/html"), (CACHE_CONTROL, "no-cache")],
    )
}

pub fn text(str: String) -> Response<Full<Bytes>> {
    let mut response = Response::new(str.into());
    *response.status_mut() = StatusCode::OK;
    response
}

pub fn ok() -> Response<Full<Bytes>> {
    let mut response = Response::default();
    *response.status_mut() = StatusCode::OK;
    response
}

pub fn redirect(uri: &str) -> Response<Full<Bytes>> {
    let mut response = Response::default();
    *response.status_mut() = StatusCode::MOVED_PERMANENTLY;
    with_headers(response, vec![(LOCATION, uri), (CACHE_CONTROL, "no-cache")])
}

pub fn not_found() -> Response<Full<Bytes>> {
    let mut response = Response::default();
    *response.status_mut() = StatusCode::NOT_FOUND;
    response
}

pub async fn file(filename: &str, content_type: &str) -> Response<Full<Bytes>> {
    if let Ok(contents) = tokio::fs::read(filename).await {
        with_headers(
            Response::new(Full::new(contents.into())),
            vec![
                (CACHE_CONTROL, "max-age=3153600000"),
                (CONTENT_TYPE, content_type),
            ],
        )
    } else {
        not_found()
    }
}