aboutsummaryrefslogtreecommitdiff
path: root/src/controller/utils.rs
blob: 544cdf8886a941a979c4a48d4dd9185fe0dd65b4 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use hyper::header::{
    HeaderName, HeaderValue, CACHE_CONTROL, CONTENT_TYPE, LOCATION, SET_COOKIE,
};
use hyper::{Body, Response, StatusCode};
use std::collections::HashMap;
use tera::{Context, Tera};
use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};
use uuid::Uuid;

use crate::controller::error;
use crate::model::config::Config;

pub fn with_header(
    response: Response<Body>,
    name: HeaderName,
    value: &str,
) -> Response<Body> {
    with_headers(response, vec![(name, value)])
}

pub fn with_headers(
    response: Response<Body>,
    headers: Vec<(HeaderName, &str)>,
) -> Response<Body> {
    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 with_login_cookie(
    config: Config,
    login_token: Uuid,
    response: Response<Body>,
) -> Response<Body> {
    let cookie = format!(
        "TOKEN={}; SameSite=Strict; HttpOnly; Max-Age=86400{}",
        login_token,
        if config.secure_cookies {
            "; Secure"
        } else {
            ""
        }
    );

    with_header(response, SET_COOKIE, &cookie)
}

pub fn with_logout_cookie(
    config: Config,
    response: Response<Body>,
) -> Response<Body> {
    let cookie = format!(
        "TOKEN=; SameSite=Strict; HttpOnly; Max-Age=0{}",
        if config.secure_cookies {
            "; Secure"
        } else {
            ""
        }
    );

    with_header(response, SET_COOKIE, &cookie)
}

pub fn template(
    assets: &HashMap<String, String>,
    templates: &Tera,
    path: &str,
    context: Context,
) -> Response<Body> {
    let mut context = context.clone();
    context.insert("assets", assets);

    let response = match templates.render(path, &context) {
        Ok(template) => Response::new(template.into()),
        Err(err) => Response::new(
            error::template(
                assets,
                templates,
                "Erreur serveur",
                &format!(
                    "Erreur lors de la préparation de la page : {:?}",
                    err
                ),
            )
            .into(),
        ),
    };

    with_headers(
        response,
        vec![(CONTENT_TYPE, "text/html"), (CACHE_CONTROL, "no-cache")],
    )
}

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

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

pub fn redirect(uri: &str) -> Response<Body> {
    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<Body> {
    let mut response = Response::default();
    *response.status_mut() = StatusCode::NOT_FOUND;
    response
}

pub async fn file(filename: &str) -> Response<Body> {
    if let Ok(file) = File::open(filename).await {
        let stream = FramedRead::new(file, BytesCodec::new());
        let body = Body::wrap_stream(stream);
        with_header(Response::new(body), CACHE_CONTROL, "max-age=3153600000")
    } else {
        not_found()
    }
}