use hyper::header::{
HeaderName, HeaderValue, CACHE_CONTROL, CONTENT_TYPE, LOCATION,
};
use hyper::{Body, Response, StatusCode};
use std::collections::HashMap;
use tera::{Context, Tera};
use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};
use crate::controller::error;
pub fn with_headers(
response: Response
,
headers: Vec<(HeaderName, &str)>,
) -> Response {
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,
templates: &Tera,
path: &str,
context: Context,
) -> Response {
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,
templates: &Tera,
msg: &str,
) -> Response {
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 {
let mut response = Response::new(str.into());
*response.status_mut() = StatusCode::OK;
response
}
pub fn ok() -> Response {
let mut response = Response::default();
*response.status_mut() = StatusCode::OK;
response
}
pub fn redirect(uri: &str) -> Response {
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 {
let mut response = Response::default();
*response.status_mut() = StatusCode::NOT_FOUND;
response
}
pub async fn file(filename: &str, content_type: &str) -> Response {
if let Ok(file) = File::open(filename).await {
let stream = FramedRead::new(file, BytesCodec::new());
let body = Body::wrap_stream(stream);
with_headers(
Response::new(body),
vec![
(CACHE_CONTROL, "max-age=3153600000"),
(CONTENT_TYPE, content_type),
],
)
} else {
not_found()
}
}