From a36f2c86a7af6e9b6cc396d61ef53386a2856c7f Mon Sep 17 00:00:00 2001 From: jakubmanczak Date: Wed, 24 Sep 2025 16:26:30 +0200 Subject: [PATCH] make a 404 page --- src/website/mod.rs | 6 +++--- src/website/pages/mod.rs | 1 + src/website/pages/notfound.rs | 19 +++++++++++++++++++ web/base.html | 20 ++++++++++++++++++++ web/footer.html | 2 +- web/notfound.html | 16 ++++++++++++++++ 6 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 src/website/pages/notfound.rs create mode 100644 web/base.html create mode 100644 web/notfound.html diff --git a/src/website/mod.rs b/src/website/mod.rs index 2116067..b8fb631 100644 --- a/src/website/mod.rs +++ b/src/website/mod.rs @@ -2,11 +2,11 @@ use std::convert::Infallible; use axum::{ body::Body, - http::{Request, StatusCode, header}, + http::{Request, header}, response::{IntoResponse, Response}, }; -use crate::website::pages::index::page_index; +use crate::website::pages::{index::page_index, notfound::page_notfound}; mod pages; @@ -22,6 +22,6 @@ pub async fn website_service(req: Request) -> Result "styles.css" => ([(header::CONTENT_TYPE, "text/css")], STYLES_CSS).into_response(), "favicon.svg" => ([(header::CONTENT_TYPE, "image/svg+xml")], FAVICON_SVG).into_response(), "favicon-x64.png" => ([(header::CONTENT_TYPE, "image/png")], FAVICON_X64).into_response(), - _ => StatusCode::NOT_FOUND.into_response(), + _ => page_notfound().await, }) } diff --git a/src/website/pages/mod.rs b/src/website/pages/mod.rs index 938b3ad..16bc55a 100644 --- a/src/website/pages/mod.rs +++ b/src/website/pages/mod.rs @@ -1,4 +1,5 @@ pub mod index; +pub mod notfound; const INTERNAL_SERVER_ERROR_MSG: &str = "An internal server error occured while rendering your HTML. Sorry!"; diff --git a/src/website/pages/notfound.rs b/src/website/pages/notfound.rs new file mode 100644 index 0000000..da7c2c1 --- /dev/null +++ b/src/website/pages/notfound.rs @@ -0,0 +1,19 @@ +use askama::Template; +use axum::{ + http::StatusCode, + response::{Html, IntoResponse, Response}, +}; + +use crate::website::pages::INTERNAL_SERVER_ERROR_MSG; + +#[derive(Template)] +#[template(path = "notfound.html")] +struct PageNotFound; + +pub async fn page_notfound() -> Response { + let a = PageNotFound; + match a.render() { + Ok(res) => (StatusCode::OK, Html(res)).into_response(), + Err(_e) => (StatusCode::INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MSG).into_response(), + } +} diff --git a/web/base.html b/web/base.html new file mode 100644 index 0000000..66e4b14 --- /dev/null +++ b/web/base.html @@ -0,0 +1,20 @@ + + + + + + + + + {% block title %}{{ title }}{% endblock %} + {% block head %} {% endblock %} + + + {% block pagecontent %} {{ pagecontent }} {% endblock %} + + diff --git a/web/footer.html b/web/footer.html index 6559087..04ebcb8 100644 --- a/web/footer.html +++ b/web/footer.html @@ -1,5 +1,5 @@