From a99ed529a12c6dc94051e19bf9e6894d718d4f0a Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 22 Feb 2019 23:29:54 +0100 Subject: [PATCH] display search errors as flash cookie --- dist/templates/index.html.hbs | 4 ++++ src/web/mod.rs | 29 ++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/dist/templates/index.html.hbs b/dist/templates/index.html.hbs index 6967d73..d09ae25 100644 --- a/dist/templates/index.html.hbs +++ b/dist/templates/index.html.hbs @@ -1,4 +1,8 @@ {{#> layout }} + {{#with error }} +

Error: {{ this }}

+ {{/with}} + {{> search-form}}

You can also upload or delete your key.

diff --git a/src/web/mod.rs b/src/web/mod.rs index 715b7fb..e0511ee 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -6,6 +6,9 @@ use rocket::response::status::Custom; use rocket::response::NamedFile; use rocket::{Outcome, State}; use rocket_contrib::templates::Template; +use rocket::response::Flash; +use rocket::request::FlashMessage; +use rocket::response::Redirect; use serde::Serialize; use handlebars::Handlebars; @@ -52,8 +55,7 @@ enum MyResponse { Plain(String), #[response(status = 500, content_type = "html")] ServerError(Template), - #[response(status = 404, content_type = "html")] - NotFound(Template), + NotFound(Flash), } impl MyResponse { @@ -84,7 +86,7 @@ impl MyResponse { commit: env!("VERGEN_SHA_SHORT").to_string(), }; - MyResponse::NotFound(Template::render("not-found", context)) + MyResponse::NotFound(Flash::error(Redirect::to("/?"), "Key not found".to_owned())) } } @@ -131,6 +133,12 @@ mod templates { pub version: String, } + #[derive(Serialize)] + pub struct Index { + pub error: Option, + pub commit: String, + pub version: String, + } #[derive(Serialize)] pub struct General { pub commit: String, @@ -504,9 +512,20 @@ fn manage() -> result::Result> { Ok(Template::render("manage", context)) } +fn error_from_flash(flash: &FlashMessage) -> Option { + if flash.name() == "error" { + Some(flash.msg().to_owned()) + } else { + None + } +} + #[get("/")] -fn root() -> Template { - let context = templates::General { +fn root( + flash: Option +) -> Template { + let context = templates::Index { + error: flash.and_then(|flash| error_from_flash(&flash)), version: env!("VERGEN_SEMVER").to_string(), commit: env!("VERGEN_SHA_SHORT").to_string(), };