From acb5ab08a85d2c752d47713336ec83848d1a34be Mon Sep 17 00:00:00 2001 From: Stefan Melmuk Date: Sun, 25 Sep 2022 04:02:16 +0200 Subject: [PATCH] add not_found catcher for 404 errors --- src/api/mod.rs | 1 + src/api/web.rs | 15 ++++++++++++++- src/main.rs | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 99fb98be..3b27c6c8 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -19,6 +19,7 @@ pub use crate::api::{ identity::routes as identity_routes, notifications::routes as notifications_routes, notifications::{start_notification_server, Notify, UpdateType}, + web::catchers as web_catchers, web::routes as web_routes, }; use crate::util; diff --git a/src/api/web.rs b/src/api/web.rs index c01e782a..2ad94db8 100644 --- a/src/api/web.rs +++ b/src/api/web.rs @@ -1,7 +1,7 @@ use std::path::{Path, PathBuf}; use rocket::serde::json::Json; -use rocket::{fs::NamedFile, http::ContentType, Route}; +use rocket::{fs::NamedFile, http::ContentType, Catcher, Route}; use serde_json::Value; use crate::{ @@ -21,6 +21,19 @@ pub fn routes() -> Vec { } } +pub fn catchers() -> Vec { + if CONFIG.web_vault_enabled() { + catchers![not_found] + } else { + catchers![] + } +} + +#[catch(404)] +async fn not_found() -> Cached> { + Cached::short(NamedFile::open(Path::new(&CONFIG.web_vault_folder()).join("404.html")).await.ok(), false) +} + #[get("/")] async fn web_index() -> Cached> { Cached::short(NamedFile::open(Path::new(&CONFIG.web_vault_folder()).join("index.html")).await.ok(), false) diff --git a/src/main.rs b/src/main.rs index ad47f3c5..b33b4fa1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -425,6 +425,7 @@ async fn launch_rocket(pool: db::DbPool, extra_debug: bool) -> Result<(), Error> .mount([basepath, "/identity"].concat(), api::identity_routes()) .mount([basepath, "/icons"].concat(), api::icons_routes()) .mount([basepath, "/notifications"].concat(), api::notifications_routes()) + .register([basepath, "/"].concat(), api::web_catchers()) .manage(pool) .manage(api::start_notification_server()) .attach(util::AppHeaders())