From a6b43e4e6da31487e555009111817b1e846109a9 Mon Sep 17 00:00:00 2001 From: Felix Wittwer Date: Tue, 23 Feb 2021 16:22:00 +0100 Subject: [PATCH] feat: add 404 page (#104) * feat: add 404 page * feat: cache 404 results for 5 minutes --- src/server/mod.rs | 5 +--- src/server/views/html/error.rs | 44 +++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index df880a9..24a3310 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -362,10 +362,7 @@ impl App { } fn not_found() -> Response { - Response::builder() - .status(StatusCode::NOT_FOUND) - .body(Body::empty()) - .unwrap() + views::html::error::render_404() } static SELF_BASE_URL: Lazy = diff --git a/src/server/views/html/error.rs b/src/server/views/html/error.rs index f42245b..f5802e2 100644 --- a/src/server/views/html/error.rs +++ b/src/server/views/html/error.rs @@ -1,6 +1,11 @@ -use hyper::{Body, Response}; +use hyper::{ + header::{CACHE_CONTROL, CONTENT_TYPE}, + Body, Response, StatusCode, +}; use maud::html; +use crate::server::assets::STATIC_STYLE_CSS_PATH; + pub fn render(title: &str, descr: &str) -> Response { super::render_html( title, @@ -20,3 +25,40 @@ pub fn render(title: &str, descr: &str) -> Response { }, ) } + +pub fn render_404() -> Response { + let rendered = html! { + html { + head { + meta charset="utf-8"; + meta name="viewport" content="width=device-width, initial-scale=1"; + title { "404 - Deps.rs" } + link rel="icon" type="image/svg+xml" href="/static/logo.svg"; + link rel="stylesheet" type="text/css" href=(STATIC_STYLE_CSS_PATH); + link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Fira+Sans:400,500,600"; + link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Source+Code+Pro"; + } + body { + section class="hero is-light" { + div class="hero-head" { (super::render_navbar()) } + } + section class="section" { + div class="container" { + div class="notification is-info" { + p class="title is-3" { "Ooops, seems like you've hit a dead end!" } + p { "The page you were looking for could not be found. In other words, this is a " b { "404 error" } "." } + } + } + } + (super::render_footer(None)) + } + } + }; + + Response::builder() + .status(StatusCode::NOT_FOUND) + .header(CONTENT_TYPE, "text/html; charset=utf-8") + .header(CACHE_CONTROL, "public, max-age=300, immutable") + .body(Body::from(rendered.0)) + .unwrap() +}