From bb906d8ecc796f6b71dda1851d6bd0aa91c6bce5 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 25 Nov 2023 08:59:39 +0100 Subject: Upgrade dependencies --- src/controller/balance.rs | 6 ++++-- src/controller/categories.rs | 20 +++++++++++--------- src/controller/error.rs | 10 ++++++++-- src/controller/incomes.rs | 23 ++++++++++++++--------- src/controller/login.rs | 14 ++++++++------ src/controller/payments.rs | 22 ++++++++++++---------- src/controller/statistics.rs | 6 ++++-- src/controller/utils.rs | 30 ++++++++++++++---------------- 8 files changed, 75 insertions(+), 56 deletions(-) (limited to 'src/controller') diff --git a/src/controller/balance.rs b/src/controller/balance.rs index adde8a9..c5d9d4a 100644 --- a/src/controller/balance.rs +++ b/src/controller/balance.rs @@ -1,4 +1,6 @@ -use hyper::{Body, Response}; +use http_body_util::Full; +use hyper::body::Bytes; +use hyper::Response; use std::collections::HashMap; use tera::Context; @@ -9,7 +11,7 @@ use crate::model::user::User; use crate::payer; use crate::templates; -pub async fn get(wallet: &Wallet) -> Response { +pub async fn get(wallet: &Wallet) -> Response> { let users = db::users::list(&wallet.pool).await; let incomes_from = db::incomes::defined_for_all(&wallet.pool).await; diff --git a/src/controller/categories.rs b/src/controller/categories.rs index b1a3664..ff2d8e7 100644 --- a/src/controller/categories.rs +++ b/src/controller/categories.rs @@ -1,4 +1,6 @@ -use hyper::{Body, Response}; +use http_body_util::Full; +use hyper::body::Bytes; +use hyper::Response; use std::collections::HashMap; use tera::Context; @@ -12,7 +14,7 @@ use crate::validation; pub async fn table( wallet: &Wallet, query: queries::Categories, -) -> Response { +) -> Response> { let categories = db::categories::list(&wallet.pool).await; let mut context = Context::new(); @@ -29,7 +31,7 @@ pub async fn table( ) } -pub async fn create_form(wallet: &Wallet) -> Response { +pub async fn create_form(wallet: &Wallet) -> Response> { create_form_feedback(wallet, HashMap::new(), None).await } @@ -37,7 +39,7 @@ async fn create_form_feedback( wallet: &Wallet, form: HashMap, error: Option, -) -> Response { +) -> Response> { let mut context = Context::new(); context.insert("header", &templates::Header::Categories); context.insert("connected_user", &wallet.user.clone()); @@ -55,7 +57,7 @@ async fn create_form_feedback( pub async fn create( wallet: &Wallet, form: HashMap, -) -> Response { +) -> Response> { let error = |e: &str| { create_form_feedback(wallet, form.clone(), Some(e.to_string())) }; @@ -73,7 +75,7 @@ pub async fn create( } } -pub async fn update_form(id: i64, wallet: &Wallet) -> Response { +pub async fn update_form(id: i64, wallet: &Wallet) -> Response> { update_form_feedback(id, wallet, HashMap::new(), None).await } @@ -82,7 +84,7 @@ async fn update_form_feedback( wallet: &Wallet, form: HashMap, error: Option, -) -> Response { +) -> Response> { let category = db::categories::get(&wallet.pool, id).await; let is_category_used = db::payments::is_category_used(&wallet.pool, id).await; @@ -108,7 +110,7 @@ pub async fn update( id: i64, wallet: &Wallet, form: HashMap, -) -> Response { +) -> Response> { let error = |e: &str| { update_form_feedback(id, wallet, form.clone(), Some(e.to_string())) }; @@ -126,7 +128,7 @@ pub async fn update( } } -pub async fn delete(id: i64, wallet: &Wallet) -> Response { +pub async fn delete(id: i64, wallet: &Wallet) -> Response> { if db::categories::delete(&wallet.pool, id).await { utils::redirect("/categories") } else { diff --git a/src/controller/error.rs b/src/controller/error.rs index 2a84255..0f6dcc1 100644 --- a/src/controller/error.rs +++ b/src/controller/error.rs @@ -1,5 +1,7 @@ +use http_body_util::Full; +use hyper::body::Bytes; use hyper::header::CACHE_CONTROL; -use hyper::{Body, Response}; +use hyper::Response; use std::collections::HashMap; use tera::{Context, Tera}; @@ -7,7 +9,11 @@ use crate::controller::utils; use crate::controller::wallet::Wallet; // TODO error code -pub fn error(wallet: &Wallet, title: &str, message: &str) -> Response { +pub fn error( + wallet: &Wallet, + title: &str, + message: &str, +) -> Response> { utils::with_headers( Response::new( template(&wallet.assets, &wallet.templates, title, message).into(), diff --git a/src/controller/incomes.rs b/src/controller/incomes.rs index cc66ed6..f22098b 100644 --- a/src/controller/incomes.rs +++ b/src/controller/incomes.rs @@ -1,6 +1,8 @@ use chrono::Datelike; use chrono::Utc; -use hyper::{Body, Response}; +use http_body_util::Full; +use hyper::body::Bytes; +use hyper::Response; use std::collections::HashMap; use tera::Context; @@ -13,7 +15,10 @@ use crate::validation; static PER_PAGE: i64 = 10; -pub async fn table(wallet: &Wallet, query: queries::Incomes) -> Response { +pub async fn table( + wallet: &Wallet, + query: queries::Incomes, +) -> Response> { let page = query.page.unwrap_or(1); let count = db::incomes::count(&wallet.pool).await; let incomes = db::incomes::list(&wallet.pool, page, PER_PAGE).await; @@ -53,7 +58,7 @@ static MONTHS: [&str; 12] = [ pub async fn create_form( wallet: &Wallet, query: queries::Incomes, -) -> Response { +) -> Response> { create_form_feedback(wallet, query, HashMap::new(), None).await } @@ -62,7 +67,7 @@ async fn create_form_feedback( query: queries::Incomes, form: HashMap, error: Option, -) -> Response { +) -> Response> { let users = db::users::list(&wallet.pool).await; let mut context = Context::new(); @@ -87,7 +92,7 @@ pub async fn create( wallet: &Wallet, query: queries::Incomes, form: HashMap, -) -> Response { +) -> Response> { let error = |e: &str| { create_form_feedback(wallet, query, form.clone(), Some(e.to_string())) }; @@ -125,7 +130,7 @@ pub async fn update_form( id: i64, wallet: &Wallet, query: queries::Incomes, -) -> Response { +) -> Response> { update_form_feedback(id, wallet, query, HashMap::new(), None).await } @@ -135,7 +140,7 @@ async fn update_form_feedback( query: queries::Incomes, form: HashMap, error: Option, -) -> Response { +) -> Response> { let users = db::users::list(&wallet.pool).await; let income = db::incomes::get(&wallet.pool, id).await; @@ -163,7 +168,7 @@ pub async fn update( wallet: &Wallet, query: queries::Incomes, form: HashMap, -) -> Response { +) -> Response> { let error = |e: &str| { update_form_feedback( id, @@ -203,7 +208,7 @@ pub async fn delete( id: i64, wallet: &Wallet, query: queries::Incomes, -) -> Response { +) -> Response> { if db::incomes::delete(&wallet.pool, id).await { utils::redirect(&format!("/incomes?page={}", query.page.unwrap_or(1))) } else { diff --git a/src/controller/login.rs b/src/controller/login.rs index 036e6fc..dd50a3a 100644 --- a/src/controller/login.rs +++ b/src/controller/login.rs @@ -1,6 +1,8 @@ use bcrypt; +use http_body_util::Full; +use hyper::body::Bytes; use hyper::header::SET_COOKIE; -use hyper::{Body, Response}; +use hyper::Response; use sqlx::sqlite::SqlitePool; use std::collections::HashMap; use tera::{Context, Tera}; @@ -18,7 +20,7 @@ pub async fn page( assets: &HashMap, templates: &Tera, error: Option<&str>, -) -> Response { +) -> Response> { let connected_user: Option = None; let mut context = Context::new(); @@ -34,7 +36,7 @@ pub async fn login( templates: &Tera, form: HashMap, pool: SqlitePool, -) -> Response { +) -> Response> { match validation::login::login(&form) { Some(login) => { match db::users::get_password_hash(&pool, login.email.clone()).await @@ -88,14 +90,14 @@ async fn server_error( assets: &HashMap, templates: &Tera, msg: &str, -) -> Response { +) -> Response> { page(assets, templates, Some(msg)).await } async fn not_authorized( assets: &HashMap, templates: &Tera, -) -> Response { +) -> Response> { page( assets, templates, @@ -104,7 +106,7 @@ async fn not_authorized( .await } -pub async fn logout(config: &Config, wallet: &Wallet) -> Response { +pub async fn logout(config: &Config, wallet: &Wallet) -> Response> { if db::users::remove_login_token(&wallet.pool, wallet.user.id).await { with_headers( utils::redirect("/"), diff --git a/src/controller/payments.rs b/src/controller/payments.rs index 2663fa7..8184015 100644 --- a/src/controller/payments.rs +++ b/src/controller/payments.rs @@ -1,5 +1,7 @@ +use http_body_util::Full; +use hyper::body::Bytes; use hyper::header::CONTENT_TYPE; -use hyper::{Body, Response}; +use hyper::Response; use std::collections::HashMap; use tera::Context; @@ -16,7 +18,7 @@ static PER_PAGE: i64 = 10; pub async fn table( wallet: &Wallet, query: queries::Payments, -) -> Response { +) -> Response> { let page = query.page.unwrap_or(1); let count = db::payments::count(&wallet.pool, &query).await; let payments = @@ -48,7 +50,7 @@ pub async fn table( pub async fn create_form( wallet: &Wallet, query: queries::Payments, -) -> Response { +) -> Response> { create_form_feedback(wallet, query, HashMap::new(), None).await } @@ -57,7 +59,7 @@ async fn create_form_feedback( query: queries::Payments, form: HashMap, error: Option, -) -> Response { +) -> Response> { let users = db::users::list(&wallet.pool).await; let categories = db::categories::list(&wallet.pool).await; @@ -82,7 +84,7 @@ pub async fn create( wallet: &Wallet, query: queries::Payments, form: HashMap, -) -> Response { +) -> Response> { let error = |e: &str| { create_form_feedback(wallet, query, form.clone(), Some(e.to_string())) }; @@ -125,7 +127,7 @@ pub async fn update_form( id: i64, wallet: &Wallet, query: queries::Payments, -) -> Response { +) -> Response> { update_form_feedback(id, wallet, query, HashMap::new(), None).await } @@ -135,7 +137,7 @@ async fn update_form_feedback( query: queries::Payments, form: HashMap, error: Option, -) -> Response { +) -> Response> { let payment = db::payments::get_for_form(&wallet.pool, id).await; let users = db::users::list(&wallet.pool).await; let categories = db::categories::list(&wallet.pool).await; @@ -164,7 +166,7 @@ pub async fn update( wallet: &Wallet, query: queries::Payments, form: HashMap, -) -> Response { +) -> Response> { let error = |e: &str| { update_form_feedback( id, @@ -207,7 +209,7 @@ pub async fn delete( id: i64, wallet: &Wallet, query: queries::Payments, -) -> Response { +) -> Response> { if db::payments::delete(&wallet.pool, id).await { let query = queries::Payments { highlight: None, @@ -229,7 +231,7 @@ pub async fn delete( pub async fn search_category( wallet: &Wallet, query: queries::PaymentCategory, -) -> Response { +) -> Response> { match db::payments::search_category(&wallet.pool, query.payment_name).await { Some(category_id) => utils::with_headers( diff --git a/src/controller/statistics.rs b/src/controller/statistics.rs index 38a5787..eb1e704 100644 --- a/src/controller/statistics.rs +++ b/src/controller/statistics.rs @@ -1,4 +1,6 @@ -use hyper::{Body, Response}; +use http_body_util::Full; +use hyper::body::Bytes; +use hyper::Response; use tera::Context; use crate::controller::utils; @@ -6,7 +8,7 @@ use crate::controller::wallet::Wallet; use crate::db; use crate::templates; -pub async fn get(wallet: &Wallet) -> Response { +pub async fn get(wallet: &Wallet) -> Response> { let categories = db::categories::list(&wallet.pool).await; let payments = db::payments::list_for_stats(&wallet.pool).await; let incomes = db::incomes::total_each_month(&wallet.pool).await; 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, + response: Response>, headers: Vec<(HeaderName, &str)>, -) -> Response { +) -> Response> { 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 { +) -> Response> { let mut context = context; context.insert("assets", assets); @@ -47,7 +47,7 @@ fn server_error( assets: &HashMap, templates: &Tera, msg: &str, -) -> Response { +) -> Response> { 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 { +pub fn text(str: String) -> Response> { let mut response = Response::new(str.into()); *response.status_mut() = StatusCode::OK; response } -pub fn ok() -> Response { +pub fn ok() -> Response> { let mut response = Response::default(); *response.status_mut() = StatusCode::OK; response } -pub fn redirect(uri: &str) -> 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 { +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); +pub async fn file(filename: &str, content_type: &str) -> Response> { + 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), -- cgit v1.2.3