From 3ab0e0fe4ef7c0e9ee867926801c08c2c78594d6 Mon Sep 17 00:00:00 2001 From: tuxmain Date: Thu, 17 Dec 2020 23:08:31 +0100 Subject: [PATCH] Title, CommonTemplate --- Cargo.lock | 4 +-- src/config.rs | 2 ++ src/server.rs | 65 ++++++++++++++++++++++---------------- src/templates.rs | 19 +++++++---- templates/admin.html | 6 ++-- templates/admin_login.html | 6 ++-- templates/index.html | 10 +++--- 7 files changed, 63 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b920977..abad67e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1626,9 +1626,9 @@ dependencies = [ [[package]] name = "subtle" -version = "2.3.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "343f3f510c2915908f155e94f17220b19ccfacf2a64a2a5d8004f2c3e311e7fd" +checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" [[package]] name = "syn" diff --git a/src/config.rs b/src/config.rs index 14aee02..f616c61 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ pub struct Config { pub cookies_domain: Option, pub listen: SocketAddr, pub root_url: String, + pub title: String, } impl Default for Config { @@ -23,6 +24,7 @@ impl Default for Config { cookies_domain: None, listen: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 10353), root_url: String::from("/"), + title: String::from("ĞMarché"), } } } diff --git a/src/server.rs b/src/server.rs index 7c859b1..e095176 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,4 +1,4 @@ -use super::{cli, config, db, queries::*, static_files, templates::*, utils::*}; +use super::{cli, config::*, db::*, queries::*, static_files, templates::*, utils::*}; use handlebars::to_json; use sha2::{Digest, Sha512Trunc256}; @@ -8,8 +8,8 @@ use std::{ }; pub async fn start_server( - config: config::Config, - dbs: db::Dbs, + config: Config, + dbs: Dbs, templates: Templates<'static>, opt: cli::MainOpt, ) { @@ -105,9 +105,9 @@ pub async fn start_server( async fn serve_index<'a>( req: tide::Request<()>, - config: Arc, + config: Arc, templates: Arc>, - dbs: db::Dbs, + dbs: Dbs, errors: &[ErrorTemplate<'a>], new_ad_form_refill: Option, ) -> tide::Result { @@ -120,9 +120,12 @@ async fn serve_index<'a>( .render( "index.html", &IndexTemplate { - lang: "fr", - root_url: &config.root_url, - errors, + common: CommonTemplate { + lang: "fr", + root_url: &config.root_url, + title: &config.title, + errors, + }, ads: &to_json( dbs.ads .iter() @@ -147,9 +150,9 @@ async fn serve_index<'a>( async fn serve_admin<'a>( req: tide::Request<()>, - config: Arc, + config: Arc, templates: Arc>, - dbs: db::Dbs, + dbs: Dbs, errors: &[ErrorTemplate<'a>], ) -> tide::Result { let selected_ad = req.param("ad").ok(); @@ -161,9 +164,12 @@ async fn serve_admin<'a>( .render( "admin.html", &AdminTemplate { - lang: "fr", - root_url: &config.root_url, - errors, + common: CommonTemplate { + lang: "fr", + root_url: &config.root_url, + title: &config.title, + errors, + }, ads: &to_json( dbs.ads .iter() @@ -187,7 +193,7 @@ async fn serve_admin<'a>( async fn serve_admin_login<'a>( _req: tide::Request<()>, - config: Arc, + config: Arc, templates: Arc>, errors: &[ErrorTemplate<'a>], ) -> tide::Result { @@ -199,9 +205,12 @@ async fn serve_admin_login<'a>( .render( "admin_login.html", &AdminLoginTemplate { - lang: "fr", - root_url: &config.root_url, - errors, + common: CommonTemplate { + lang: "fr", + root_url: &config.root_url, + title: &config.title, + errors, + }, }, ) .unwrap_or_else(|e| e.to_string()), @@ -211,9 +220,9 @@ async fn serve_admin_login<'a>( async fn handle_post_index( mut req: tide::Request<()>, - config: Arc, + config: Arc, templates: Arc>, - dbs: db::Dbs, + dbs: Dbs, ) -> tide::Result { match req.body_form::().await? { Query::NewAdQuery(query) => { @@ -227,9 +236,9 @@ async fn handle_post_index( async fn handle_new_ad( req: tide::Request<()>, - config: Arc, + config: Arc, templates: Arc>, - dbs: db::Dbs, + dbs: Dbs, query: NewAdQuery, ) -> tide::Result { let mut hasher = Sha512Trunc256::new(); @@ -279,9 +288,9 @@ async fn handle_new_ad( async fn handle_rm_ad( req: tide::Request<()>, - config: Arc, + config: Arc, templates: Arc>, - dbs: db::Dbs, + dbs: Dbs, query: RmAdQuery, ) -> tide::Result { let mut hasher = Sha512Trunc256::new(); @@ -330,9 +339,9 @@ async fn handle_rm_ad( async fn handle_admin( req: tide::Request<()>, - config: Arc, + config: Arc, templates: Arc>, - dbs: db::Dbs, + dbs: Dbs, ) -> tide::Result { if let Some(psw) = req.cookie("admin") { if config.admin_passwords.contains(&String::from(psw.value())) { @@ -355,9 +364,9 @@ async fn handle_admin( async fn handle_post_admin( mut req: tide::Request<()>, - config: Arc, + config: Arc, templates: Arc>, - dbs: db::Dbs, + dbs: Dbs, ) -> tide::Result { if let Some(psw) = req.cookie("admin") { if config.admin_passwords.contains(&String::from(psw.value())) { @@ -414,7 +423,7 @@ async fn handle_post_admin( async fn handle_admin_logout( req: tide::Request<()>, - config: Arc, + config: Arc, ) -> tide::Result { let mut r: tide::Response = tide::Redirect::new("/").into(); if let Some(mut cookie) = req.cookie("admin") { diff --git a/src/templates.rs b/src/templates.rs index 375664f..9f3c2f1 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -47,26 +47,31 @@ pub fn load_templates<'reg>(dir: &Path) -> Templates<'reg> { } #[derive(Serialize)] -pub struct IndexTemplate<'a> { +pub struct CommonTemplate<'a> { pub lang: &'a str, pub root_url: &'a str, + pub title: &'a str, pub errors: &'a [ErrorTemplate<'a>], +} + +#[derive(Serialize)] +pub struct IndexTemplate<'a> { + #[serde(flatten)] + pub common: CommonTemplate<'a>, pub ads: &'a Json, pub new_ad_form_refill: Option, } #[derive(Serialize)] pub struct AdminLoginTemplate<'a> { - pub lang: &'a str, - pub root_url: &'a str, - pub errors: &'a [ErrorTemplate<'a>], + #[serde(flatten)] + pub common: CommonTemplate<'a>, } #[derive(Serialize)] pub struct AdminTemplate<'a> { - pub lang: &'a str, - pub root_url: &'a str, - pub errors: &'a [ErrorTemplate<'a>], + #[serde(flatten)] + pub common: CommonTemplate<'a>, pub ads: &'a Json, } diff --git a/templates/admin.html b/templates/admin.html index ddd34db..ebd7f1a 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -2,7 +2,7 @@ - Administration | ĞMarché + Administration | {{title}} @@ -10,11 +10,11 @@
- +
-

Administration – ĞMarché

+

Administration – {{title}}

{{#if errors}}
diff --git a/templates/admin_login.html b/templates/admin_login.html index 7273ae7..613fb7e 100644 --- a/templates/admin_login.html +++ b/templates/admin_login.html @@ -2,18 +2,18 @@ - Administration | ĞMarché + Administration | {{title}}
- +
-

Administration – ĞMarché

+

Administration – {{title}}

Authentification

diff --git a/templates/index.html b/templates/index.html index 9fa0b3f..1c556d5 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,7 +2,7 @@ - ĞMarché + {{title}} @@ -10,11 +10,11 @@
- +
-

ĞMarché

+

{{title}}

{{#if errors}}
@@ -27,9 +27,7 @@
{{/if}} -

Ceci est une démo du nouveau site ĞMarché en développement. C'est très moche et il n'y a pas tellement de fonctionnalités mais ça avance. ;)

- -

Merci de ne pas encore utiliser ce site pour de vrais événements !

+

Ceci est une démo du nouveau site ĞMarché en développement.

{{#if ads}} Cliquez sur une annonce pour afficher le détail.