added 404 custom page

pull/1/head
kirbylife 2019-11-23 13:05:41 -06:00
parent a56dfcb749
commit ac70178423
3 changed files with 49 additions and 0 deletions

View File

@ -19,6 +19,7 @@ use comrak::{markdown_to_html, ComrakOptions};
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use dotenv::dotenv; use dotenv::dotenv;
use rand::Rng; use rand::Rng;
use rocket::Request;
use rocket_contrib::serve::StaticFiles; use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
use std::env; use std::env;
@ -115,10 +116,26 @@ fn show_post(title: String) -> Template {
Template::render("post", context) 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() { fn main() {
rocket::ignite() rocket::ignite()
.attach(Template::fairing()) .attach(Template::fairing())
.mount("/", routes![index, show_post]) .mount("/", routes![index, show_post])
.mount("/static", StaticFiles::from("static")) .mount("/static", StaticFiles::from("static"))
.register(catchers![not_found_404])
.launch(); .launch();
} }

19
static/css/404.css 100644
View File

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

View File

@ -0,0 +1,13 @@
{% extends "base" %}
{% block extracss %}
<link href="/static/css/404.css" rel="stylesheet"/>
{% endblock extracss %}
{% block content %}
<h1>404</h1>
<span>
=> Error: No matching routes for GET {{ url }} <br/>
=> Warning: Responding with 404 Not Found catcher.
</span>
{% endblock content %}