Skip to content

Commit

Permalink
use a custom 404 page
Browse files Browse the repository at this point in the history
to customize the 404 page you can copy the handlebar template
`src/static/templates/404.hbs` to the TEMPLATES_FOLDER (defaults to
`data/templates/`)
  • Loading branch information
stefan0xC committed Dec 2, 2022
1 parent 8feed29 commit 9790193
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 5 deletions.
313 changes: 313 additions & 0 deletions resources/404.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 10 additions & 5 deletions src/api/web.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::path::{Path, PathBuf};

use rocket::serde::json::Json;
use rocket::{fs::NamedFile, http::ContentType, Catcher, Route};
use rocket::{fs::NamedFile, http::ContentType, response::content::RawHtml as Html, serde::json::Json, Catcher, Route};
use serde_json::Value;

use crate::{
api::core::now,
api::{core::now, ApiResult},
error::Error,
util::{Cached, SafeString},
CONFIG,
Expand All @@ -30,8 +29,13 @@ pub fn catchers() -> Vec<Catcher> {
}

#[catch(404)]
async fn not_found() -> Cached<Option<NamedFile>> {
Cached::short(NamedFile::open(Path::new(&CONFIG.web_vault_folder()).join("404.html")).await.ok(), false)
fn not_found() -> ApiResult<Html<String>> {
// Return the page
let json = json!({
"urlpath": CONFIG.domain_path()
});
let text = CONFIG.render_template("404", &json)?;
Ok(Html(text))
}

#[get("/")]
Expand Down Expand Up @@ -91,6 +95,7 @@ fn alive(_conn: DbConn) -> Json<String> {
#[get("/vw_static/<filename>")]
pub fn static_files(filename: String) -> Result<(ContentType, &'static [u8]), Error> {
match filename.as_ref() {
"404.png" => Ok((ContentType::PNG, include_bytes!("../static/images/404.png"))),
"mail-github.png" => Ok((ContentType::PNG, include_bytes!("../static/images/mail-github.png"))),
"logo-gray.png" => Ok((ContentType::PNG, include_bytes!("../static/images/logo-gray.png"))),
"error-x.svg" => Ok((ContentType::SVG, include_bytes!("../static/images/error-x.svg"))),
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,8 @@ where
reg!("admin/organizations");
reg!("admin/diagnostics");

reg!("404");

// And then load user templates to overwrite the defaults
// Use .hbs extension for the files
// Templates get registered with their relative name
Expand Down
Binary file added src/static/images/404.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 9790193

Please sign in to comment.