Browse Source

Set up tide and tera

master
Dylan Baker 3 years ago
parent
commit
7f7dd6f5ce
6 changed files with 1932 additions and 2 deletions
  1. 1
    0
      .gitignore
  2. 1874
    0
      Cargo.lock
  3. 5
    0
      Cargo.toml
  4. 35
    2
      src/main.rs
  5. 1
    0
      templates/admin.html
  6. 16
    0
      templates/layout.html

+ 1
- 0
.gitignore View File

@@ -1 +1,2 @@
1 1
 /target
2
+.env

+ 1874
- 0
Cargo.lock
File diff suppressed because it is too large
View File


+ 5
- 0
Cargo.toml View File

@@ -7,3 +7,8 @@ edition = "2018"
7 7
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8 8
 
9 9
 [dependencies]
10
+async-std = { version = "1.6.0", features = ["attributes"] }
11
+dotenv = "0.15.0"
12
+tide = "0.13.0"
13
+tera = "1.5.0"
14
+tide-tera = "0.1.1"

+ 35
- 2
src/main.rs View File

@@ -1,3 +1,36 @@
1
-fn main() {
2
-    println!("Hello, world!");
1
+use std::io::ErrorKind;
2
+
3
+use tera::{Context, Tera};
4
+use tide::utils::After;
5
+use tide::{Body, Response, Result, StatusCode};
6
+
7
+#[async_std::main]
8
+async fn main() -> Result<()> {
9
+    tide::log::start();
10
+    let mut app = tide::new();
11
+
12
+    app.with(After(|mut res: Response| async {
13
+        if let Some(err) = res.downcast_error::<async_std::io::Error>() {
14
+            if let ErrorKind::NotFound = err.kind() {
15
+                res.set_status(StatusCode::NotFound);
16
+            }
17
+        }
18
+
19
+        Ok(res)
20
+    }));
21
+
22
+    app.with(After(|mut res: Response| async {
23
+        res.set_content_type(tide::http::mime::HTML);
24
+        Ok(res)
25
+    }));
26
+
27
+    app.at("/admin").get(|_| async {
28
+        let tera = Tera::new("templates/**/*.html")?;
29
+        let html = tera.render("admin.html", &Context::new())?;
30
+        Ok(Body::from_string(html))
31
+    });
32
+
33
+    app.listen("127.0.0.1:8080").await?;
34
+
35
+    Ok(())
3 36
 }

+ 1
- 0
templates/admin.html View File

@@ -0,0 +1 @@
1
+{% extends "layout.html" %} {% block content %} {% endblock %}

+ 16
- 0
templates/layout.html View File

@@ -0,0 +1,16 @@
1
+<!DOCTYPE html>
2
+<html lang="en">
3
+  <head>
4
+    <meta charset="UTF-8" />
5
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+    <title>Microblog Admin</title>
7
+    <style>
8
+      body {
9
+        background: #aab5a9;
10
+      }
11
+    </style>
12
+  </head>
13
+  <body>
14
+    {% block content %} {% endblock %}
15
+  </body>
16
+</html>

Loading…
Cancel
Save