aboutsummaryrefslogtreecommitdiff
path: root/src/controller/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/controller/utils.rs')
-rw-r--r--src/controller/utils.rs30
1 files changed, 14 insertions, 16 deletions
diff --git a/src/controller/utils.rs b/src/controller/utils.rs
index fb1d9ad..1b58c68 100644
--- a/src/controller/utils.rs
+++ b/src/controller/utils.rs
@@ -1,18 +1,18 @@
+use http_body_util::Full;
+use hyper::body::Bytes;
use hyper::header::{
HeaderName, HeaderValue, CACHE_CONTROL, CONTENT_TYPE, LOCATION,
};
-use hyper::{Body, Response, StatusCode};
+use hyper::{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<Body>,
+ response: Response<Full<Bytes>>,
headers: Vec<(HeaderName, &str)>,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let mut response = response;
let response_headers = response.headers_mut();
for (name, value) in headers {
@@ -26,7 +26,7 @@ pub fn template(
templates: &Tera,
path: &str,
context: Context,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
let mut context = context;
context.insert("assets", assets);
@@ -47,7 +47,7 @@ fn server_error(
assets: &HashMap<String, String>,
templates: &Tera,
msg: &str,
-) -> Response<Body> {
+) -> Response<Full<Bytes>> {
with_headers(
Response::new(
error::template(assets, templates, "Erreur serveur", msg).into(),
@@ -56,36 +56,34 @@ fn server_error(
)
}
-pub fn text(str: String) -> Response<Body> {
+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<Body> {
+pub fn ok() -> Response<Full<Bytes>> {
let mut response = Response::default();
*response.status_mut() = StatusCode::OK;
response
}
-pub fn redirect(uri: &str) -> Response<Body> {
+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<Body> {
+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<Body> {
- if let Ok(file) = File::open(filename).await {
- let stream = FramedRead::new(file, BytesCodec::new());
- let body = Body::wrap_stream(stream);
+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(body),
+ Response::new(Full::new(contents.into())),
vec![
(CACHE_CONTROL, "max-age=3153600000"),
(CONTENT_TYPE, content_type),