From 7c3aab8515f0e607d9c316a1e4dc0647ec58195b Mon Sep 17 00:00:00 2001 From: blek Date: Sat, 21 Oct 2023 01:49:36 +1000 Subject: [PATCH] pass config to views --- filed/src/main.rs | 2 +- filed/src/web/forms.rs | 5 ++++- filed/src/web/mod.rs | 7 ++++--- filed/src/web/pages.rs | 43 ++++++++++++++++++++++++++++-------------- filed/src/web/state.rs | 6 ++++-- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/filed/src/main.rs b/filed/src/main.rs index 56ae7d1..903c7c9 100644 --- a/filed/src/main.rs +++ b/filed/src/main.rs @@ -28,5 +28,5 @@ async fn main() { femme::with_level(femme::LevelFilter::Off); } - web::serve(envy).await; + web::serve(envy, conf).await; } diff --git a/filed/src/web/forms.rs b/filed/src/web/forms.rs index 7a32c43..da66853 100644 --- a/filed/src/web/forms.rs +++ b/filed/src/web/forms.rs @@ -53,7 +53,8 @@ pub async fn upload(form: FormData, state: SharedState) -> Result warp::reply::with_status( warp::reply::html( BadActionReq { - env: state.env.clone() + env: state.env.clone(), + conf: state.config.clone() } .render() .map_err(|err| warp::reject::custom(HttpReject::AskamaError(err.into())))? @@ -93,6 +94,7 @@ pub async fn upload(form: FormData, state: SharedState) -> Result warp::reply::html( pages::ErrorPage { env: state.env, + conf: state.config.clone(), error_text: "You must consent to the terms and conditions!".into(), link: Some("/".into()), link_text: Some("Go back".into()) @@ -152,6 +154,7 @@ pub async fn upload(form: FormData, state: SharedState) -> Result let uploaded = UploadSuccessPage { env: state.env.clone(), + conf: state.config.clone(), link: file.leftmost_link() }; diff --git a/filed/src/web/mod.rs b/filed/src/web/mod.rs index 8645279..e307f47 100644 --- a/filed/src/web/mod.rs +++ b/filed/src/web/mod.rs @@ -7,7 +7,7 @@ use std::env::current_dir; use static_dir::static_dir; use warp::{Filter, reply::Reply, reject::Rejection}; -use crate::{env::Env, files::lookup::FileManager}; +use crate::{env::Env, files::lookup::FileManager, config::types::Config}; mod pages; mod forms; @@ -33,7 +33,7 @@ pub fn routes(state: SharedState) -> impl Filter, pub link_text: Option @@ -87,7 +96,8 @@ pub async fn uploaded(query: HashMap, state: SharedState) -> Res let rendered = Uploaded { file: query.get("file").unwrap().clone(), - env: state.env.clone() + env: state.env.clone(), + conf: state.config.clone() }; Ok(warp::reply::html(rendered.render().map_err(|err| warp::reject::custom(HttpReject::AskamaError(err)))?)) } @@ -103,7 +113,8 @@ pub fn uploaded_f(state: SharedState) -> impl Filter Result, Rejection> { let rendered = Index { - env: state.env.clone() + env: state.env.clone(), + conf: state.config.clone() }; Ok(warp::reply::html(rendered.render().map_err(|err| warp::reject::custom(HttpReject::AskamaError(err)))?)) } @@ -116,7 +127,8 @@ pub fn index_f(state: SharedState) -> impl Filter Result, Rejection> { let rendered = PasswordedFilesHelpPage { - env: state.env.clone() + env: state.env.clone(), + conf: state.config.clone() }; Ok(warp::reply::html(rendered.render().map_err(|err| warp::reject::custom(HttpReject::AskamaError(err)))?)) } @@ -130,7 +142,8 @@ pub fn passworded_f(state: SharedState) -> impl Filter Result, Rejection> { let rendered = AuthorsPage { - env: state.env + env: state.env.clone(), + conf: state.config.clone() }; Ok(warp::reply::html(rendered.render().map_err(|err| warp::reject::custom(HttpReject::AskamaError(err)))?)) } @@ -144,7 +157,8 @@ pub fn authors_f(state: SharedState) -> impl Filter Result, Rejection> { let rendered = LicensePage { - env: state.env + env: state.env.clone(), + conf: state.config.clone() }; Ok(warp::reply::html(rendered.render().map_err(|err| warp::reject::custom(HttpReject::AskamaError(err)))?)) } @@ -158,7 +172,8 @@ pub fn license_f(state: SharedState) -> impl Filter Result, Rejection> { let rendered = TOSPage { - env: state.env + env: state.env.clone(), + conf: state.config.clone() }; Ok(warp::reply::html(rendered.render().map_err(|err| warp::reject::custom(HttpReject::AskamaError(err)))?)) } diff --git a/filed/src/web/state.rs b/filed/src/web/state.rs index 21a6140..5246dda 100644 --- a/filed/src/web/state.rs +++ b/filed/src/web/state.rs @@ -1,11 +1,13 @@ use redis::Client; -use crate::{env::Env, files::lookup::FileManager}; +use crate::{env::Env, files::lookup::FileManager, config::types::Config}; #[derive(Debug, Clone)] pub struct SharedState { pub redis_cli: Client, + pub file_mgr: FileManager, + pub env: Env, - pub file_mgr: FileManager + pub config: Config }