|
@@ -1,19 +1,17 @@
|
1
|
|
-use std::env;
|
2
|
1
|
use std::fs::File;
|
3
|
2
|
use std::io::prelude::*;
|
4
|
3
|
use std::io::{Error, ErrorKind};
|
5
|
4
|
use std::path::PathBuf;
|
6
|
5
|
|
7
|
|
-use chrono::prelude::Local;
|
8
|
6
|
use dotenv;
|
9
|
7
|
use serde::{Deserialize, Serialize};
|
10
|
8
|
use serde_json;
|
11
|
|
-use tera::{Context, Tera};
|
|
9
|
+use tera::Context;
|
12
|
10
|
use tide::utils::After;
|
13
|
|
-use tide::{Body, Response, StatusCode};
|
14
|
|
-use uuid::Uuid;
|
|
11
|
+use tide::{Response, StatusCode};
|
15
|
12
|
|
16
|
13
|
mod fs;
|
|
14
|
+mod routes;
|
17
|
15
|
|
18
|
16
|
#[derive(Debug, Serialize, Deserialize)]
|
19
|
17
|
pub struct Post {
|
|
@@ -23,12 +21,6 @@ pub struct Post {
|
23
|
21
|
date: String,
|
24
|
22
|
}
|
25
|
23
|
|
26
|
|
-#[derive(Debug, Serialize, Deserialize)]
|
27
|
|
-struct User {
|
28
|
|
- username: String,
|
29
|
|
- password: String,
|
30
|
|
-}
|
31
|
|
-
|
32
|
24
|
impl Post {
|
33
|
25
|
async fn save(&mut self) -> std::io::Result<()> {
|
34
|
26
|
let mut path: PathBuf = fs::get_posts_directory_path().await?;
|
|
@@ -52,12 +44,6 @@ impl Post {
|
52
|
44
|
}
|
53
|
45
|
}
|
54
|
46
|
|
55
|
|
-fn render(template: &str, context: &Context) -> Result<Body, tide::Error> {
|
56
|
|
- let tera = Tera::new("templates/**/*.html")?;
|
57
|
|
- let html = tera.render(template, &context)?;
|
58
|
|
- Ok(Body::from_string(html))
|
59
|
|
-}
|
60
|
|
-
|
61
|
47
|
#[async_std::main]
|
62
|
48
|
async fn main() -> std::io::Result<()> {
|
63
|
49
|
dotenv::dotenv().ok();
|
|
@@ -75,13 +61,13 @@ async fn main() -> std::io::Result<()> {
|
75
|
61
|
} else {
|
76
|
62
|
StatusCode::InternalServerError
|
77
|
63
|
};
|
78
|
|
- res.set_body(render("error.html", &context)?);
|
|
64
|
+ res.set_body(routes::render_template("error.html", &context)?);
|
79
|
65
|
res.set_status(status);
|
80
|
66
|
}
|
81
|
67
|
None => match res.status() {
|
82
|
68
|
StatusCode::NotFound => {
|
83
|
69
|
context.insert("error", "Page not found");
|
84
|
|
- res.set_body(render("error.html", &context)?);
|
|
70
|
+ res.set_body(routes::render_template("error.html", &context)?);
|
85
|
71
|
}
|
86
|
72
|
_ => {}
|
87
|
73
|
},
|
|
@@ -105,82 +91,12 @@ async fn main() -> std::io::Result<()> {
|
105
|
91
|
.as_bytes(),
|
106
|
92
|
));
|
107
|
93
|
|
108
|
|
- app.at("/").get(|req: tide::Request<()>| async move {
|
109
|
|
- let posts: Vec<Post> = fs::get_all_posts().await?;
|
110
|
|
- let mut context = Context::new();
|
111
|
|
- context.insert("posts", &posts);
|
112
|
|
- let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
|
113
|
|
- context.insert("logged_in", &logged_in);
|
114
|
|
- render("index.html", &context)
|
115
|
|
- });
|
116
|
|
-
|
117
|
|
- app.at("/posts")
|
118
|
|
- .post(|mut req: tide::Request<()>| async move {
|
119
|
|
- let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
|
120
|
|
- if !logged_in {
|
121
|
|
- Ok(tide::Redirect::new("/login"))
|
122
|
|
- } else {
|
123
|
|
- let mut post: Post = req.body_form().await?;
|
124
|
|
- post.id = Uuid::new_v4().to_string();
|
125
|
|
- post.date = Local::now().date().naive_local().to_string();
|
126
|
|
- post.body = post.body.trim().to_owned();
|
127
|
|
- post.save().await?;
|
128
|
|
- Ok(tide::Redirect::new("/"))
|
129
|
|
- }
|
130
|
|
- });
|
131
|
|
-
|
132
|
|
- app.at("/posts/:id")
|
133
|
|
- .get(|req: tide::Request<()>| async move {
|
134
|
|
- let mut context = Context::new();
|
135
|
|
- let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
|
136
|
|
- context.insert("logged_in", &logged_in);
|
137
|
|
- let post_id = req.param("id")?;
|
138
|
|
- let post = fs::get_one_post(post_id).await?;
|
139
|
|
- context.insert("post", &post);
|
140
|
|
- render("single.html", &context)
|
141
|
|
- });
|
142
|
|
-
|
143
|
|
- app.at("/login")
|
144
|
|
- .get(|mut req: tide::Request<()>| async move {
|
145
|
|
- let mut context = Context::new();
|
146
|
|
- let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
|
147
|
|
- context.insert("logged_in", &logged_in);
|
148
|
|
- match req.session_mut().get::<String>("flash_error") {
|
149
|
|
- Some(error) => {
|
150
|
|
- req.session_mut().remove("flash_error");
|
151
|
|
- &context.insert("error", &error);
|
152
|
|
- }
|
153
|
|
- None => {}
|
154
|
|
- }
|
155
|
|
- render("login.html", &context)
|
156
|
|
- })
|
157
|
|
- .post(|mut req: tide::Request<()>| async move {
|
158
|
|
- let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
|
159
|
|
- if logged_in {
|
160
|
|
- return Ok(tide::Redirect::new("/"));
|
161
|
|
- }
|
162
|
|
-
|
163
|
|
- let username = env::var("ADMIN_USERNAME")?;
|
164
|
|
- let password = env::var("ADMIN_PASSWORD")?;
|
165
|
|
- let user: User = req.body_form().await?;
|
166
|
|
- if user.username == username && user.password == password {
|
167
|
|
- req.session_mut().remove("logged_in");
|
168
|
|
- req.session_mut().insert("logged_in", true)?;
|
169
|
|
- Ok(tide::Redirect::new("/"))
|
170
|
|
- } else {
|
171
|
|
- req.session_mut().remove("logged_in");
|
172
|
|
- req.session_mut()
|
173
|
|
- .insert("flash_error", "Invalid credentials")?;
|
174
|
|
- Ok(tide::Redirect::new("/login"))
|
175
|
|
- }
|
176
|
|
- });
|
177
|
|
-
|
178
|
|
- app.at("/logout")
|
179
|
|
- .post(|mut req: tide::Request<()>| async move {
|
180
|
|
- req.session_mut().remove("logged_in");
|
181
|
|
- req.session_mut().insert("logged_in", false)?;
|
182
|
|
- Ok(tide::Redirect::new("/"))
|
183
|
|
- });
|
|
94
|
+ app.at("/").get(routes::index);
|
|
95
|
+ app.at("/posts").post(routes::create_post);
|
|
96
|
+ app.at("/posts/:id").get(routes::single_post);
|
|
97
|
+ app.at("/posts/:id/edit").get(routes::edit_post);
|
|
98
|
+ app.at("/login").get(routes::login_page).post(routes::login);
|
|
99
|
+ app.at("/logout").post(routes::logout);
|
184
|
100
|
|
185
|
101
|
app.listen("127.0.0.1:8080").await?;
|
186
|
102
|
|