Browse Source

Read posts dir

master
Dylan Baker 3 years ago
parent
commit
8a24093c9a
3 changed files with 56 additions and 4 deletions
  1. 22
    4
      src/main.rs
  2. 11
    0
      templates/admin/index.html
  3. 23
    0
      templates/admin/layout.html

+ 22
- 4
src/main.rs View File

@@ -1,10 +1,11 @@
1 1
 use std::env;
2
-use std::fs::File;
2
+use std::fs::{read_dir, File};
3 3
 use std::io::prelude::*;
4 4
 use std::io::{Error, ErrorKind};
5 5
 use std::path::PathBuf;
6 6
 
7
-use chrono::prelude::{Local, NaiveDate};
7
+use async_std::fs::read_to_string;
8
+use chrono::prelude::Local;
8 9
 use dotenv;
9 10
 use serde::{Deserialize, Serialize};
10 11
 use serde_json;
@@ -40,7 +41,7 @@ impl Post {
40 41
     }
41 42
 }
42 43
 
43
-fn get_posts_directory() -> Result<PathBuf, std::io::Error> {
44
+fn get_posts_directory() -> Result<PathBuf, Error> {
44 45
     match env::var("POSTS_DIR") {
45 46
         Ok(dir) => Ok(dir.into()),
46 47
         Err(_) => Err(Error::new(
@@ -50,6 +51,20 @@ fn get_posts_directory() -> Result<PathBuf, std::io::Error> {
50 51
     }
51 52
 }
52 53
 
54
+async fn read_all_posts() -> Result<Vec<Post>, Error> {
55
+    let path = get_posts_directory()?;
56
+    let mut posts: Vec<Post> = vec![];
57
+
58
+    for file in read_dir(path)? {
59
+        let file = file?;
60
+        let contents = read_to_string(file.path()).await?;
61
+        let post: Post = serde_json::from_str(&contents)?;
62
+        posts.push(post);
63
+    }
64
+
65
+    Ok(posts)
66
+}
67
+
53 68
 #[async_std::main]
54 69
 async fn main() -> std::io::Result<()> {
55 70
     dotenv::dotenv().ok();
@@ -74,7 +89,10 @@ async fn main() -> std::io::Result<()> {
74 89
 
75 90
     app.at("/admin").get(|_| async {
76 91
         let tera = Tera::new("templates/**/*.html")?;
77
-        let html = tera.render("admin/index.html", &Context::new())?;
92
+        let posts = read_all_posts().await?;
93
+        let mut context = Context::new();
94
+        context.insert("posts", &posts);
95
+        let html = tera.render("admin/index.html", &context)?;
78 96
         Ok(Body::from_string(html))
79 97
     });
80 98
 

+ 11
- 0
templates/admin/index.html View File

@@ -15,4 +15,15 @@
15 15
     <input class="form__button" type="submit" value="Post" />
16 16
   </div>
17 17
 </form>
18
+
19
+<div class="posts">
20
+  {% for post in posts %}
21
+  <div class="posts__post post">
22
+    <h3 class="post__heading">{{ post.title }} // {{ post.date }}</h3>
23
+    <div class="post__body">
24
+      {{ post.body }}
25
+    </div>
26
+  </div>
27
+  {% endfor %}
28
+</div>
18 29
 {% endblock %}

+ 23
- 0
templates/admin/layout.html View File

@@ -13,6 +13,7 @@
13 13
 
14 14
       body {
15 15
         background: #aab5a9;
16
+        color: #383e37;
16 17
         font-family: Verdana, Geneva, Tahoma, sans-serif;
17 18
         font-size: 12px;
18 19
       }
@@ -49,6 +50,28 @@
49 50
         padding: 0.25em 1em;
50 51
       }
51 52
 
53
+      .post {
54
+        margin: 4em 0;
55
+      }
56
+
57
+      .post__heading {
58
+        background: white;
59
+        border: 1px solid #383e37;
60
+        margin: 1em 0;
61
+        padding: 0.25em;
62
+      }
63
+
64
+      .post__heading {
65
+        font-size: 12px;
66
+        font-style: italic;
67
+      }
68
+
69
+      .post__heading::before {
70
+        content: ':: ';
71
+        font-style: normal;
72
+        font-weight: normal;
73
+      }
74
+
52 75
       @media (max-width: 500px) {
53 76
         .form__button {
54 77
           width: 100%;

Loading…
Cancel
Save