diff --git a/src/server.rs b/src/server.rs index b5915f4..21425f3 100644 --- a/src/server.rs +++ b/src/server.rs @@ -121,28 +121,31 @@ pub async fn start_server( .and(warp::get()) .and(warp::fs::dir(opt.dir.0.join(static_files::STATIC_DIR))); - let handle_new_ad = - { - let handle_index = handle_index.clone(); - let dbs = dbs.clone(); - let cache_ads = cache_ads.clone(); - move |query: NewAdQuery| { - let mut hasher = Sha512Trunc256::new(); - hasher.update(query.psw); - dbs.ads + let handle_new_ad = { + let handle_index = handle_index.clone(); + let dbs = dbs.clone(); + let cache_ads = cache_ads.clone(); + move |query: NewAdQuery| { + let mut hasher = Sha512Trunc256::new(); + hasher.update(query.psw); + dbs.ads .insert( AdId::random(), bincode::serialize(&Ad { author: query.author, password: hasher.finalize()[..].try_into().unwrap(), price: query.price, - pubkey: (!query.pubkey.is_empty()).then_some(match format_pubkey(&query.pubkey) { + pubkey: if query.pubkey.is_empty() { + None + } else { + Some(match format_pubkey(&query.pubkey) { Ok(pubkey) => pubkey, Err(e) => return handle_index(&[ErrorTemplate{text: match e { PubkeyDecodeError::BadChecksum => "La somme de contrôle de la clé publique est incorrecte.", PubkeyDecodeError::BadFormat => "Le format de la clé publique est incorrect.", }}]) - }), + }) + }, quantity: query.quantity, time: 0, title: query.title, @@ -150,24 +153,24 @@ pub async fn start_server( .unwrap(), ) .unwrap(); - dbs.ads.flush().unwrap(); - 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()?, - }) + dbs.ads.flush().unwrap(); + 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::>(), - ); - drop(cache_ads); - redirect_302("/").into_response() - } - }; + }) + .collect::>(), + ); + drop(cache_ads); + redirect_302("/").into_response() + } + }; let handle_rm_ad = { let handle_index = handle_index.clone();