From ac70178423b960224b5aafd306cd8c72000049c8 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Sat, 23 Nov 2019 13:05:41 -0600 Subject: [PATCH] added 404 custom page --- src/main.rs | 17 +++++++++++++++++ static/css/404.css | 19 +++++++++++++++++++ templates/404.html.tera | 13 +++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 static/css/404.css create mode 100644 templates/404.html.tera diff --git a/src/main.rs b/src/main.rs index 75a3b65..43e556a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ use comrak::{markdown_to_html, ComrakOptions}; use diesel::pg::PgConnection; use dotenv::dotenv; use rand::Rng; +use rocket::Request; use rocket_contrib::serve::StaticFiles; use rocket_contrib::templates::Template; use std::env; @@ -115,10 +116,26 @@ fn show_post(title: String) -> Template { Template::render("post", context) } +#[catch(404)] +fn not_found_404(req: &Request) -> Template { + let mut context = Context::new(); + + let mut rng = rand::thread_rng(); + let title_fmt = TITLES[rng.gen_range(0, TITLES.len())]; + let title = str::replace(title_fmt, "{}", "CódigoComentado"); + + let uri = format!("{}", req.uri()); + + context.insert("title", &title); + context.insert("url", &uri); + Template::render("404", context) +} + fn main() { rocket::ignite() .attach(Template::fairing()) .mount("/", routes![index, show_post]) .mount("/static", StaticFiles::from("static")) + .register(catchers![not_found_404]) .launch(); } diff --git a/static/css/404.css b/static/css/404.css new file mode 100644 index 0000000..aa97c49 --- /dev/null +++ b/static/css/404.css @@ -0,0 +1,19 @@ +.container { + display: grid; + grid-template-columns: 1fr auto 1fr; + grid-template-rows: 1fr 2fr; + grid-template-areas: + ". title ." + ". info ."; +} + +.container h1 { + grid-area: title; + text-align: center; +} + +.container span { + grid-area: info; + font-family: monospace; + font-size: 20px; +} diff --git a/templates/404.html.tera b/templates/404.html.tera new file mode 100644 index 0000000..465e080 --- /dev/null +++ b/templates/404.html.tera @@ -0,0 +1,13 @@ +{% extends "base" %} + +{% block extracss %} + +{% endblock extracss %} + +{% block content %} +

404

+ + => Error: No matching routes for GET {{ url }}
+ => Warning: Responding with 404 Not Found catcher. +
+{% endblock content %}