|
@@ -53,13 +53,19 @@ fn get_posts_directory() -> Result<PathBuf, Error> {
|
53
|
53
|
async fn read_all_posts() -> Result<Vec<Post>, Error> {
|
54
|
54
|
let path = match get_posts_directory() {
|
55
|
55
|
Ok(p) => Ok(p),
|
56
|
|
- Err(_) => Err(Error::new(ErrorKind::Other, "POSTS_DIR variable is not set"))
|
|
56
|
+ Err(_) => Err(Error::new(
|
|
57
|
+ ErrorKind::Other,
|
|
58
|
+ "POSTS_DIR variable is not set",
|
|
59
|
+ )),
|
57
|
60
|
}?;
|
58
|
61
|
let mut posts: Vec<Post> = vec![];
|
59
|
62
|
|
60
|
63
|
let files = match read_dir(path) {
|
61
|
64
|
Ok(f) => Ok(f),
|
62
|
|
- Err(_) => Err(Error::new(ErrorKind::Other, "Posts directory does not exist"))
|
|
65
|
+ Err(_) => Err(Error::new(
|
|
66
|
+ ErrorKind::Other,
|
|
67
|
+ "Posts directory does not exist",
|
|
68
|
+ )),
|
63
|
69
|
}?;
|
64
|
70
|
|
65
|
71
|
for file in files {
|
|
@@ -67,11 +73,17 @@ async fn read_all_posts() -> Result<Vec<Post>, Error> {
|
67
|
73
|
if let Some("json") = file.path().extension().and_then(OsStr::to_str) {
|
68
|
74
|
let contents = match read_to_string(file.path()).await {
|
69
|
75
|
Ok(c) => Ok(c),
|
70
|
|
- Err(_) => Err(Error::new(ErrorKind::Other, format!("Error reading post {:?}", file.path())))
|
|
76
|
+ Err(_) => Err(Error::new(
|
|
77
|
+ ErrorKind::Other,
|
|
78
|
+ format!("Error reading post {:?}", file.path()),
|
|
79
|
+ )),
|
71
|
80
|
}?;
|
72
|
81
|
let mut post: Post = match serde_json::from_str(&contents) {
|
73
|
82
|
Ok(p) => Ok(p),
|
74
|
|
- Err(_) => Err(Error::new(ErrorKind::Other, format!("Error deserializing post")))
|
|
83
|
+ Err(_) => Err(Error::new(
|
|
84
|
+ ErrorKind::Other,
|
|
85
|
+ format!("Error deserializing post"),
|
|
86
|
+ )),
|
75
|
87
|
}?;
|
76
|
88
|
post.body = post.body.replace("\n", "<br>");
|
77
|
89
|
posts.push(post);
|
|
@@ -172,16 +184,18 @@ async fn main() -> std::io::Result<()> {
|
172
|
184
|
Ok(tide::Redirect::new("/"))
|
173
|
185
|
} else {
|
174
|
186
|
req.session_mut().remove("logged_in");
|
175
|
|
- req.session_mut().insert("flash_error", "Invalid credentials")?;
|
|
187
|
+ req.session_mut()
|
|
188
|
+ .insert("flash_error", "Invalid credentials")?;
|
176
|
189
|
Ok(tide::Redirect::new("/login"))
|
177
|
190
|
}
|
178
|
191
|
});
|
179
|
192
|
|
180
|
|
- app.at("/logout").get(|mut req: tide::Request<()>| async move {
|
181
|
|
- req.session_mut().remove("logged_in");
|
182
|
|
- req.session_mut().insert("logged_in", false)?;
|
183
|
|
- Ok(tide::Redirect::new("/"))
|
184
|
|
- });
|
|
193
|
+ app.at("/logout")
|
|
194
|
+ .get(|mut req: tide::Request<()>| async move {
|
|
195
|
+ req.session_mut().remove("logged_in");
|
|
196
|
+ req.session_mut().insert("logged_in", false)?;
|
|
197
|
+ Ok(tide::Redirect::new("/"))
|
|
198
|
+ });
|
185
|
199
|
|
186
|
200
|
app.listen("127.0.0.1:8080").await?;
|
187
|
201
|
|