Bladeren bron

Read posts dir

master
Dylan Baker 3 jaren geleden
bovenliggende
commit
8a24093c9a
3 gewijzigde bestanden met toevoegingen van 56 en 4 verwijderingen
  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 Bestand weergeven

1
 use std::env;
1
 use std::env;
2
-use std::fs::File;
2
+use std::fs::{read_dir, File};
3
 use std::io::prelude::*;
3
 use std::io::prelude::*;
4
 use std::io::{Error, ErrorKind};
4
 use std::io::{Error, ErrorKind};
5
 use std::path::PathBuf;
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
 use dotenv;
9
 use dotenv;
9
 use serde::{Deserialize, Serialize};
10
 use serde::{Deserialize, Serialize};
10
 use serde_json;
11
 use serde_json;
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
     match env::var("POSTS_DIR") {
45
     match env::var("POSTS_DIR") {
45
         Ok(dir) => Ok(dir.into()),
46
         Ok(dir) => Ok(dir.into()),
46
         Err(_) => Err(Error::new(
47
         Err(_) => Err(Error::new(
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
 #[async_std::main]
68
 #[async_std::main]
54
 async fn main() -> std::io::Result<()> {
69
 async fn main() -> std::io::Result<()> {
55
     dotenv::dotenv().ok();
70
     dotenv::dotenv().ok();
74
 
89
 
75
     app.at("/admin").get(|_| async {
90
     app.at("/admin").get(|_| async {
76
         let tera = Tera::new("templates/**/*.html")?;
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
         Ok(Body::from_string(html))
96
         Ok(Body::from_string(html))
79
     });
97
     });
80
 
98
 

+ 11
- 0
templates/admin/index.html Bestand weergeven

15
     <input class="form__button" type="submit" value="Post" />
15
     <input class="form__button" type="submit" value="Post" />
16
   </div>
16
   </div>
17
 </form>
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
 {% endblock %}
29
 {% endblock %}

+ 23
- 0
templates/admin/layout.html Bestand weergeven

13
 
13
 
14
       body {
14
       body {
15
         background: #aab5a9;
15
         background: #aab5a9;
16
+        color: #383e37;
16
         font-family: Verdana, Geneva, Tahoma, sans-serif;
17
         font-family: Verdana, Geneva, Tahoma, sans-serif;
17
         font-size: 12px;
18
         font-size: 12px;
18
       }
19
       }
49
         padding: 0.25em 1em;
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
       @media (max-width: 500px) {
75
       @media (max-width: 500px) {
53
         .form__button {
76
         .form__button {
54
           width: 100%;
77
           width: 100%;

Laden…
Annuleren
Opslaan