add a 404 template and page
This commit is contained in:
parent
225c67fb4c
commit
88e8c43052
23
src/main.rs
23
src/main.rs
|
@ -2,26 +2,37 @@
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
|
|
||||||
|
use rocket::Request;
|
||||||
use rocket_contrib::templates::Template;
|
use rocket_contrib::templates::Template;
|
||||||
|
|
||||||
#[derive(serde::Serialize)]
|
#[derive(serde::Serialize)]
|
||||||
struct Context {
|
struct IndexContext {
|
||||||
name: String,
|
name: &'static str,
|
||||||
items: Vec<&'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("/")]
|
#[get("/")]
|
||||||
fn index() -> Template {
|
fn index() -> Template {
|
||||||
let c = String::from("home");
|
let context = IndexContext { name: "home", items: vec!["One", "Two", "Three"] };
|
||||||
let context = Context { name: c, items: vec!["One", "Two", "Three"] };
|
|
||||||
Template::render("index", &context)
|
Template::render("index", &context)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
rocket::ignite()
|
rocket::ignite()
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.mount("/", routes![index]).launch();
|
.mount("/", routes![index])
|
||||||
|
.register(catchers![not_found])
|
||||||
|
.launch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,14 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<title>404</title>
|
<title>{{ title }}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>404: Hey! There's nothing here.</h1>
|
<h1>404: Hey! There's nothing here.</h1>
|
||||||
The page at {{ path }} does not exist!
|
The page at {{ path }} does not exist!
|
||||||
|
The request was
|
||||||
|
<code><pre>
|
||||||
|
{{ req }}
|
||||||
|
</pre></code>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue