pass config to views

This commit is contained in:
blek 2023-10-21 01:49:36 +10:00
parent 04b52bde74
commit 7c3aab8515
Signed by: blek
GPG Key ID: 14546221E3595D0C
5 changed files with 42 additions and 21 deletions

View File

@ -28,5 +28,5 @@ async fn main() {
femme::with_level(femme::LevelFilter::Off); femme::with_level(femme::LevelFilter::Off);
} }
web::serve(envy).await; web::serve(envy, conf).await;
} }

View File

@ -53,7 +53,8 @@ pub async fn upload(form: FormData, state: SharedState) -> Result<Box<dyn Reply>
warp::reply::with_status( warp::reply::with_status(
warp::reply::html( warp::reply::html(
BadActionReq { BadActionReq {
env: state.env.clone() env: state.env.clone(),
conf: state.config.clone()
} }
.render() .render()
.map_err(|err| warp::reject::custom(HttpReject::AskamaError(err.into())))? .map_err(|err| warp::reject::custom(HttpReject::AskamaError(err.into())))?
@ -93,6 +94,7 @@ pub async fn upload(form: FormData, state: SharedState) -> Result<Box<dyn Reply>
warp::reply::html( warp::reply::html(
pages::ErrorPage { pages::ErrorPage {
env: state.env, env: state.env,
conf: state.config.clone(),
error_text: "You must consent to the terms and conditions!".into(), error_text: "You must consent to the terms and conditions!".into(),
link: Some("/".into()), link: Some("/".into()),
link_text: Some("Go back".into()) link_text: Some("Go back".into())
@ -152,6 +154,7 @@ pub async fn upload(form: FormData, state: SharedState) -> Result<Box<dyn Reply>
let uploaded = UploadSuccessPage { let uploaded = UploadSuccessPage {
env: state.env.clone(), env: state.env.clone(),
conf: state.config.clone(),
link: file.leftmost_link() link: file.leftmost_link()
}; };

View File

@ -7,7 +7,7 @@ use std::env::current_dir;
use static_dir::static_dir; use static_dir::static_dir;
use warp::{Filter, reply::Reply, reject::Rejection}; 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 pages;
mod forms; mod forms;
@ -33,7 +33,7 @@ pub fn routes(state: SharedState) -> impl Filter<Extract = impl Reply, Error = R
/* /*
Serve the HTTP server Serve the HTTP server
*/ */
pub async fn serve(env: Env) { pub async fn serve(env: Env, config: Config) {
log::info!("Listening on {}", env.listen.to_string()); log::info!("Listening on {}", env.listen.to_string());
@ -41,7 +41,8 @@ pub async fn serve(env: Env) {
let state = SharedState { let state = SharedState {
redis_cli: redis_cli.clone(), redis_cli: redis_cli.clone(),
env: env.clone(), env: env.clone(),
file_mgr: FileManager::new(redis_cli, env.clone()) file_mgr: FileManager::new(redis_cli, env.clone()),
config: config.clone()
}; };
warp::serve(routes(state)).run(env.listen).await; warp::serve(routes(state)).run(env.listen).await;

View File

@ -7,20 +7,22 @@ use std::collections::HashMap;
use warp::{reply::{Reply, Html}, Filter, reject::Rejection}; use warp::{reply::{Reply, Html}, Filter, reject::Rejection};
use askama::Template; use askama::Template;
use crate::env::Env; use crate::{env::Env, config::types::Config};
use super::{state::SharedState, rejection::HttpReject}; use super::{state::SharedState, rejection::HttpReject};
#[derive(Template)] #[derive(Template)]
#[template( path = "index.html" )] #[template( path = "index.html" )]
pub struct Index { pub struct Index {
pub env: Env pub env: Env,
pub conf: Config
} }
#[derive(Template)] #[derive(Template)]
#[template( path = "bad_action_req.html" )] #[template( path = "bad_action_req.html" )]
pub struct BadActionReq { pub struct BadActionReq {
pub env: Env pub env: Env,
pub conf: Config
} }
#[derive(Template)] #[derive(Template)]
@ -28,14 +30,16 @@ pub struct BadActionReq {
#[allow(dead_code)] #[allow(dead_code)]
pub struct Uploaded { pub struct Uploaded {
file: String, file: String,
pub env: Env pub env: Env,
pub conf: Config
} }
#[derive(Template)] #[derive(Template)]
#[template( path = "passworded-files.html" )] #[template( path = "passworded-files.html" )]
#[allow(dead_code)] #[allow(dead_code)]
pub struct PasswordedFilesHelpPage { pub struct PasswordedFilesHelpPage {
pub env: Env pub env: Env,
pub conf: Config
} }
#[derive(Template)] #[derive(Template)]
@ -43,6 +47,7 @@ pub struct PasswordedFilesHelpPage {
#[allow(dead_code)] #[allow(dead_code)]
pub struct UploadSuccessPage { pub struct UploadSuccessPage {
pub env: Env, pub env: Env,
pub conf: Config,
pub link: String pub link: String
} }
@ -51,21 +56,24 @@ pub struct UploadSuccessPage {
#[template( path = "authors.html" )] #[template( path = "authors.html" )]
#[allow(dead_code)] #[allow(dead_code)]
pub struct AuthorsPage { pub struct AuthorsPage {
pub env: Env pub env: Env,
pub conf: Config
} }
#[derive(Template)] #[derive(Template)]
#[template( path = "license.html" )] #[template( path = "license.html" )]
#[allow(dead_code)] #[allow(dead_code)]
pub struct LicensePage { pub struct LicensePage {
pub env: Env pub env: Env,
pub conf: Config
} }
#[derive(Template)] #[derive(Template)]
#[template( path = "tos.html" )] #[template( path = "tos.html" )]
#[allow(dead_code)] #[allow(dead_code)]
pub struct TOSPage { pub struct TOSPage {
pub env: Env pub env: Env,
pub conf: Config
} }
@ -74,6 +82,7 @@ pub struct TOSPage {
#[allow(dead_code)] #[allow(dead_code)]
pub struct ErrorPage { pub struct ErrorPage {
pub env: Env, pub env: Env,
pub conf: Config,
pub error_text: String, pub error_text: String,
pub link: Option<String>, pub link: Option<String>,
pub link_text: Option<String> pub link_text: Option<String>
@ -87,7 +96,8 @@ pub async fn uploaded(query: HashMap<String, String>, state: SharedState) -> Res
let rendered = Uploaded { let rendered = Uploaded {
file: query.get("file").unwrap().clone(), 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)))?)) 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<Extract = impl Reply, Error
pub async fn index(state: SharedState) -> Result<Html<String>, Rejection> { pub async fn index(state: SharedState) -> Result<Html<String>, Rejection> {
let rendered = Index { 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)))?)) 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<Extract = impl Reply, Error =
pub async fn passworded(state: SharedState) -> Result<Html<String>, Rejection> { pub async fn passworded(state: SharedState) -> Result<Html<String>, Rejection> {
let rendered = PasswordedFilesHelpPage { 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)))?)) 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<Extract = impl Reply, Err
pub async fn authors(state: SharedState) -> Result<Html<String>, Rejection> { pub async fn authors(state: SharedState) -> Result<Html<String>, Rejection> {
let rendered = AuthorsPage { 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)))?)) 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<Extract = impl Reply, Error
pub async fn license(state: SharedState) -> Result<Html<String>, Rejection> { pub async fn license(state: SharedState) -> Result<Html<String>, Rejection> {
let rendered = LicensePage { 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)))?)) 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<Extract = impl Reply, Error
pub async fn tos(state: SharedState) -> Result<Html<String>, Rejection> { pub async fn tos(state: SharedState) -> Result<Html<String>, Rejection> {
let rendered = TOSPage { 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)))?)) Ok(warp::reply::html(rendered.render().map_err(|err| warp::reject::custom(HttpReject::AskamaError(err)))?))
} }

View File

@ -1,11 +1,13 @@
use redis::Client; use redis::Client;
use crate::{env::Env, files::lookup::FileManager}; use crate::{env::Env, files::lookup::FileManager, config::types::Config};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SharedState { pub struct SharedState {
pub redis_cli: Client, pub redis_cli: Client,
pub file_mgr: FileManager,
pub env: Env, pub env: Env,
pub file_mgr: FileManager pub config: Config
} }