add a 404 template and page

This commit is contained in:
la Fleur 2020-11-08 00:37:05 +01:00
parent 225c67fb4c
commit 88e8c43052
2 changed files with 22 additions and 7 deletions

View File

@ -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();
}

View File

@ -2,10 +2,14 @@
<html>
<head>
<meta charset="utf-8" />
<title>404</title>
<title>{{ title }}</title>
</head>
<body>
<h1>404: Hey! There's nothing here.</h1>
The page at {{ path }} does not exist!
The request was
<code><pre>
{{ req }}
</pre></code>
</body>
</html>