errors: add hagrid templated error handler

This commit is contained in:
Vincent Breitmoser 2019-07-11 22:38:16 +02:00
parent 9f4b82a9b1
commit dd6c69f11d
No known key found for this signature in database
GPG Key ID: 7BD18320DEADFA11
3 changed files with 35 additions and 5 deletions

View File

@ -0,0 +1,5 @@
{{#> layout }}
<h2>Error 503</h2>
<tt>Whoa there! Slow down a little!</tt>
{{/layout}}

View File

@ -100,6 +100,16 @@ location /pks/lookup {
proxy_pass http://127.0.0.1:8080;
}
location /errors {
internal;
proxy_pass http://127.0.0.1:8080;
}
location /search {
error_page 503 /errors/503/rate-limit-web;
proxy_pass http://127.0.0.1:8080;
}
location /pks {
proxy_pass http://127.0.0.1:8080;
}
@ -112,10 +122,6 @@ location /verify {
proxy_pass http://127.0.0.1:8080;
}
location /search {
proxy_pass http://127.0.0.1:8080;
}
location /upload {
proxy_pass http://127.0.0.1:8080;
}

View File

@ -1,5 +1,5 @@
use rocket;
use rocket::http::Header;
use rocket::http::{Header, Status};
use rocket::request;
use rocket::outcome::Outcome;
use rocket::response::NamedFile;
@ -7,6 +7,7 @@ use rocket::config::Config;
use rocket_contrib::templates::Template;
use rocket::http::uri::Uri;
use rocket_contrib::json::JsonValue;
use rocket::response::status::Custom;
use serde::Serialize;
use handlebars::Handlebars;
@ -310,6 +311,23 @@ fn stats() -> Template {
Template::render("about/stats", templates::General::default())
}
#[get("/errors/<code>/<template>")]
fn errors(
code: u16,
template: String,
) -> Result<Custom<Template>> {
if !template.chars().all(|x| x == '-' || char::is_ascii_alphabetic(&x)) {
return Err(failure::err_msg("bad request"));
}
let status_code = Status::from_code(code)
.ok_or(failure::err_msg("bad request"))?;
let response_body = Template::render(
format!("errors/{}-{}", code, template),
templates::General::default()
);
Ok(Custom(status_code, response_body))
}
pub fn serve() -> Result<()> {
Err(rocket_factory(rocket::ignite())?.launch().into())
}
@ -326,6 +344,7 @@ fn rocket_factory(rocket: rocket::Rocket) -> Result<rocket::Rocket> {
usage,
files,
stats,
errors,
// VKSv1
vks_api::vks_v1_by_email,
vks_api::vks_v1_by_fingerprint,