aboutsummaryrefslogtreecommitdiff
path: root/src/controller/utils.rs
blob: 1b58c68c56293cf59d656d4baf55520e79bc994c (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
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 tera::{Context, Tera};

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: &Tera,
    path: &str,
    context: Context,
) -> Response<Full<Bytes>> {
    let mut context = context;
    context.insert("assets", assets);

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

fn server_error(
    assets: &HashMap<String, String>,
    templates: &Tera,
    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()
    }
}