From 11660e7d9d25a6f5f5fe5e305bc0c01df4de4af1 Mon Sep 17 00:00:00 2001 From: tuxmain Date: Mon, 2 Nov 2020 11:41:55 +0100 Subject: [PATCH] Errors --- src/server.rs | 128 ++++++++++++++++++++++++------------------- static/style1.css | 9 +++ templates/index.html | 11 ++++ 3 files changed, 91 insertions(+), 57 deletions(-) diff --git a/src/server.rs b/src/server.rs index 1f142e9..daa92bb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -2,7 +2,7 @@ use super::{cli, config, db, static_files, templates}; use handlebars::to_json; use serde::{Deserialize, Serialize}; -use serde_json::value::{Map, Value as Json}; +use serde_json::value::Value as Json; use sha2::{Digest, Sha512Trunc256}; use std::{ convert::{TryFrom, TryInto}, @@ -43,35 +43,35 @@ pub async fn start_server( ) { let templates = Arc::new(templates); - let mut data = Map::::new(); - data.insert("lang".into(), to_json("fr")); - data.insert( - "ads".into(), - to_json( - dbs.ads - .iter() - .filter_map(|x| { - let (ad_id, ad) = x.ok()?; - Some(AdWithId { - id: hex::encode(ad_id.as_ref()), - ad: bincode::deserialize::(&ad).ok()?, - }) + let cache_ads = Arc::new(RwLock::new(to_json( + dbs.ads + .iter() + .filter_map(|x| { + let (ad_id, ad) = x.ok()?; + Some(AdWithId { + id: hex::encode(ad_id.as_ref()), + ad: bincode::deserialize::(&ad).ok()?, }) - .collect::>(), - ), - ); - - let data = Arc::new(RwLock::new(data)); + }) + .collect::>(), + ))); let dbs = Arc::new(dbs); let handle_index = { - let data = data.clone(); - move || { + let cache_ads = cache_ads.clone(); + move |errors: &[ErrorTemplate]| { warp::reply::html( templates .hb - .render("index.html", &*data.read().unwrap()) + .render( + "index.html", + &IndexTemplate { + lang: "fr", + errors, + ads: &*cache_ads.read().unwrap(), + }, + ) .unwrap_or_else(|e| e.to_string()), ) } @@ -84,7 +84,7 @@ pub async fn start_server( let handle_new_ad = { let handle_index = handle_index.clone(); let dbs = dbs.clone(); - let data = data.clone(); + let cache_ads = cache_ads.clone(); move |query: NewAdQuery| { let mut hasher = Sha512Trunc256::new(); hasher.update(query.psw); @@ -102,22 +102,21 @@ pub async fn start_server( ) .unwrap(); dbs.ads.flush().unwrap(); - data.write().unwrap().insert( - "ads".into(), - to_json( - dbs.ads - .iter() - .filter_map(|x| { - let (ad_id, ad) = x.ok()?; - Some(AdWithId { - id: hex::encode(ad_id.as_ref()), - ad: bincode::deserialize::(&ad).ok()?, - }) + let mut cache_ads = cache_ads.write().unwrap(); + *cache_ads = to_json( + dbs.ads + .iter() + .filter_map(|x| { + let (ad_id, ad) = x.ok()?; + Some(AdWithId { + id: hex::encode(ad_id.as_ref()), + ad: bincode::deserialize::(&ad).ok()?, }) - .collect::>(), - ), + }) + .collect::>(), ); - handle_index() + drop(cache_ads); + handle_index(&[]) } }; @@ -148,41 +147,56 @@ pub async fn start_server( dbs.ads.remove(&ad_id).unwrap(); dbs.ads.flush().unwrap(); - data.write().unwrap().insert( - "ads".into(), - to_json( - dbs.ads - .iter() - .filter_map(|x| { - let (ad_id, ad) = x.ok()?; - Some(AdWithId { - id: hex::encode(ad_id.as_ref()), - ad: bincode::deserialize::(&ad).ok()?, - }) + let mut cache_ads = cache_ads.write().unwrap(); + *cache_ads = to_json( + dbs.ads + .iter() + .filter_map(|x| { + let (ad_id, ad) = x.ok()?; + Some(AdWithId { + id: hex::encode(ad_id.as_ref()), + ad: bincode::deserialize::(&ad).ok()?, }) - .collect::>(), - ), + }) + .collect::>(), ); + } else { + return handle_index(&[ErrorTemplate {text: "Le mot de passe de l'annonce est incorrect."}]); } } } } } - handle_index() + handle_index(&[]) } }; - let route_index = warp::path::end().and(warp::get().map(handle_index).or(warp::post().and( - warp::body::form::().map(move |query: Query| match query { - Query::NewAdQuery(query) => handle_new_ad(query), - Query::RmAdQuery(query) => handle_rm_ad(query), - }), - ))); + let route_index = warp::path::end().and(warp::get().map(move || handle_index(&[])).or( + warp::post().and( + warp::body::form::().map(move |query: Query| match query { + Query::NewAdQuery(query) => handle_new_ad(query), + Query::RmAdQuery(query) => handle_rm_ad(query), + }), + ), + )); warp::serve(route_static.or(route_index)) .run(config.listen) .await; } + +#[derive(Serialize)] +struct IndexTemplate<'a> { + lang: &'a str, + errors: &'a [ErrorTemplate<'a>], + ads: &'a Json, +} + +#[derive(Serialize)] +struct ErrorTemplate<'a> { + text: &'a str, +} + #[derive(Clone, Debug, Deserialize)] struct NewAdQuery { author: String, diff --git a/static/style1.css b/static/style1.css index bc3d6c2..b173443 100644 --- a/static/style1.css +++ b/static/style1.css @@ -12,6 +12,15 @@ html, body { width: 200px; } +#errors { + padding: 4px; + border: 2px dashed rgba(128, 0, 0, 0.2); + border-radius: 3px; + font-weight: bold; + background-color: rgba(255,0,0,0.2); + color: #800000; +} + .center { padding: 4px; } diff --git a/templates/index.html b/templates/index.html index 2938434..b81523d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -12,6 +12,17 @@

ĞMarché

+ {{#if errors}} +
+ Oups, il y a un problème : +
    + {{#each errors}} +
  • {{this.text}}
  • + {{/each}} +
+
+ {{/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. ;)