From 88e8c430527a557990e1a1846692f929ce1376b1 Mon Sep 17 00:00:00 2001 From: lafleur Date: Sun, 8 Nov 2020 00:37:05 +0100 Subject: [PATCH] add a 404 template and page --- src/main.rs | 23 +++++++++++++++++------ templates/error/404.html.tera | 6 +++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index b755737..999a20d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,26 +2,37 @@ #[macro_use] extern crate rocket; +use rocket::Request; use rocket_contrib::templates::Template; #[derive(serde::Serialize)] -struct Context { - name: String, +struct IndexContext { + name: &'static str, items: Vec<&'static str> } - +#[catch(404)] +fn not_found(req: &Request<'_>) -> Template { + println!("{:#?}", req); + let dbg = format!("{:?}", req); + let mut map = std::collections::HashMap::new(); + map.insert("title", "Page not found"); + map.insert("path", req.uri().path()); + map.insert("req", &dbg); + Template::render("error/404", &map) +} #[get("/")] fn index() -> Template { - let c = String::from("home"); - let context = Context { name: c, items: vec!["One", "Two", "Three"] }; + let context = IndexContext { name: "home", items: vec!["One", "Two", "Three"] }; Template::render("index", &context) } fn main() { rocket::ignite() .attach(Template::fairing()) - .mount("/", routes![index]).launch(); + .mount("/", routes![index]) + .register(catchers![not_found]) + .launch(); } diff --git a/templates/error/404.html.tera b/templates/error/404.html.tera index d1c0b36..d2739a6 100644 --- a/templates/error/404.html.tera +++ b/templates/error/404.html.tera @@ -2,10 +2,14 @@ - 404 + {{ title }}

404: Hey! There's nothing here.

The page at {{ path }} does not exist! + The request was +
+{{ req }}
+