maintenance: return json errors for json API

This commit is contained in:
Vincent Breitmoser 2019-06-23 00:09:15 +02:00
parent dceffa6d9e
commit 0c2bf30779
No known key found for this signature in database
GPG Key ID: 7BD18320DEADFA11
2 changed files with 30 additions and 9 deletions

View File

@ -36,10 +36,13 @@ impl Fairing for MaintenanceMode {
};
let path = request.uri().path();
if self.is_relevant_path_api(path) || request.method() == Method::Put {
request.set_uri(uri!(maintenance_error_api: message));
if self.is_request_json(path) {
request.set_uri(uri!(maintenance_error_json: message));
request.set_method(Method::Get);
} else if self.is_relevant_path_web(path) {
} else if self.is_request_plain(path, request.method()) {
request.set_uri(uri!(maintenance_error_plain: message));
request.set_method(Method::Get);
} else if self.is_request_web(path) {
request.set_uri(uri!(maintenance_error_web: message));
request.set_method(Method::Get);
}
@ -51,12 +54,16 @@ impl MaintenanceMode {
MaintenanceMode { maintenance_file }
}
fn is_relevant_path_api(&self, path: &str) -> bool {
fn is_request_json(&self, path: &str) -> bool {
path.starts_with("/vks/v1/upload") ||
path.starts_with("/pks/add")
path.starts_with("/vks/v1/request-verify")
}
fn is_relevant_path_web(&self, path: &str) -> bool {
fn is_request_plain(&self, path: &str, method: Method) -> bool {
path.starts_with("/pks/add") || method == Method::Put
}
fn is_request_web(&self, path: &str) -> bool {
path.starts_with("/upload") ||
path.starts_with("/manage") ||
path.starts_with("/verify")
@ -70,11 +77,21 @@ impl MaintenanceMode {
}
}
#[get("/maintenance/api/<message>")]
pub fn maintenance_error_api(message: String) -> MyResponse {
#[get("/maintenance/plain/<message>")]
pub fn maintenance_error_plain(message: String) -> MyResponse {
MyResponse::MaintenancePlain(message)
}
#[derive(Serialize)]
struct JsonErrorMessage {
message: String,
}
#[get("/maintenance/json/<message>")]
pub fn maintenance_error_json(message: String) -> MyResponse {
MyResponse::MaintenanceJson(json!(JsonErrorMessage{ message }))
}
#[get("/maintenance/web/<message>")]
pub fn maintenance_error_web(message: String) -> MyResponse {
let ctx = templates::MaintenanceMode{

View File

@ -4,6 +4,7 @@ use rocket::response::NamedFile;
use rocket::config::Config;
use rocket_contrib::templates::Template;
use rocket::http::uri::Uri;
use rocket_contrib::json::JsonValue;
use serde::Serialize;
use handlebars::Handlebars;
@ -53,6 +54,8 @@ pub enum MyResponse {
BadRequestPlain(String),
#[response(status = 503, content_type = "html")]
Maintenance(Template),
#[response(status = 503, content_type = "json")]
MaintenanceJson(JsonValue),
#[response(status = 503, content_type = "plain")]
MaintenancePlain(String),
}
@ -315,8 +318,9 @@ fn rocket_factory(rocket: rocket::Rocket) -> Result<rocket::Rocket> {
manage::vks_manage_post,
manage::vks_manage_unpublish,
// Maintenance error page
maintenance::maintenance_error_api,
maintenance::maintenance_error_web,
maintenance::maintenance_error_json,
maintenance::maintenance_error_plain,
];
let db_service = configure_db_service(rocket.config())?;