Browse Source

Explicit 404 page

master
Dylan Baker 5 years ago
parent
commit
dfff9ae831
4 changed files with 49 additions and 17 deletions
  1. 2
    1
      src/main.rs
  2. 9
    2
      src/routes.rs
  3. 27
    0
      templates/404.html.tera
  4. 11
    14
      templates/snippets/show.tera

+ 2
- 1
src/main.rs View File

@@ -23,6 +23,7 @@ mod schema;
23 23
 mod snippet;
24 24
 
25 25
 use crate::routes::static_rocket_catch_info_for_bad_request;
26
+use crate::routes::static_rocket_catch_info_for_not_found;
26 27
 use crate::routes::static_rocket_route_info_for_create_snippet;
27 28
 use crate::routes::static_rocket_route_info_for_index;
28 29
 use crate::routes::static_rocket_route_info_for_show_raw_snippet;
@@ -37,6 +38,6 @@ fn main() {
37 38
             "/",
38 39
             routes![index, show_snippet, show_raw_snippet, create_snippet],
39 40
         )
40
-        .register(catchers![bad_request])
41
+        .register(catchers![bad_request, not_found])
41 42
         .launch();
42 43
 }

+ 9
- 2
src/routes.rs View File

@@ -41,9 +41,10 @@ pub fn show_snippet(id: i32, connection: DbConn) -> Template {
41 41
             .map(|line| line.to_string())
42 42
             .collect();
43 43
         context.insert("context", Context { snippet, lines });
44
+        Template::render("snippets/show", &context)
45
+    } else {
46
+        Template::render("404", &context)
44 47
     }
45
-
46
-    Template::render("snippets/show", &context)
47 48
 }
48 49
 
49 50
 #[get("/snippets/<id>/raw")]
@@ -81,3 +82,9 @@ pub fn bad_request(req: &Request) -> String {
81 82
         },
82 83
     )
83 84
 }
85
+
86
+#[catch(404)]
87
+pub fn not_found() -> Template {
88
+    let context: HashMap<(), ()> = HashMap::new();
89
+    Template::render("404", &context)
90
+}

+ 27
- 0
templates/404.html.tera View File

@@ -0,0 +1,27 @@
1
+<html>
2
+  <head>
3
+    <title>404 not found</title>
4
+    <meta name="viewport" content="width=device-width">
5
+    <style>
6
+      .container {
7
+        margin: auto;
8
+        max-width: 800;
9
+      }
10
+
11
+      .header,
12
+      .body {
13
+        padding: 0 15px;
14
+      }
15
+    </style>
16
+  </head>
17
+  <body>
18
+    <div class="container">
19
+      <h1 class="header">
20
+        404 not found
21
+      </h1>
22
+      <p class="body">
23
+        <a href="/">home</a>
24
+      </p>
25
+    </div>
26
+  </body>
27
+</html>

+ 11
- 14
templates/snippets/show.tera View File

@@ -1,6 +1,7 @@
1
+{% set snippet = context.snippet %}
1 2
 <html>
2 3
   <head>
3
-    <title>{% if context %}{{ context.snippet.title }}{% else %}404 - snippet not found{% endif %}</title>
4
+    <title>{{ snippet.title }}</title>
4 5
     <style>
5 6
       .container {
6 7
         margin: auto;
@@ -11,7 +12,7 @@
11 12
         align-items: baseline;
12 13
         display: flex;
13 14
         justify-content: space-between;
14
-        padding: 15px;
15
+        padding: 0 15px;
15 16
       }
16 17
 
17 18
       .header__raw-link {
@@ -40,20 +41,16 @@
40 41
   <body>
41 42
     <div class="container">
42 43
       <h1 class="header">
43
-        {% if context %}
44
-          {{ context.snippet.title }}
45
-          <a class="header__raw-link" href="/snippets/{{ context.snippet.id }}/raw">raw</a>
46
-        {% else %}
47
-          404 - snippet not found
48
-        {% endif %}
44
+        {{ snippet.title }}
45
+        <a class="header__raw-link" href="/snippets/{{ snippet.id }}/raw">
46
+          raw
47
+        </a>
49 48
       </h1>
50
-      {% if context %}
51
-        <div class="body">
52
-          {% set length = context.lines | length - 1 %}
53
-          {{ context.lines | first | safe }}{% for line in context.lines | slice(start=1, end=length) %}<code id="line-{{ loop.index }}">{{ line | safe }}</code>
49
+      <div class="body">
50
+        {% set length = context.lines | length - 1 %}
51
+        {{ context.lines | first | safe }}{% for line in context.lines | slice(start=1, end=length) %}<code id="line-{{ loop.index }}">{{ line | safe }}</code>
54 52
 {% endfor %}</pre>
55
-        </div>
56
-      {% endif %}
53
+      </div>
57 54
     </div>
58 55
   </body>
59 56
 </html>

Loading…
Cancel
Save