|
@@ -23,6 +23,12 @@ struct Post {
|
23
|
23
|
date: String,
|
24
|
24
|
}
|
25
|
25
|
|
|
26
|
+#[derive(Debug, Serialize, Deserialize)]
|
|
27
|
+struct User {
|
|
28
|
+ username: String,
|
|
29
|
+ password: String,
|
|
30
|
+}
|
|
31
|
+
|
26
|
32
|
impl Post {
|
27
|
33
|
fn save(&mut self) -> std::io::Result<()> {
|
28
|
34
|
let mut path: PathBuf = get_posts_directory()?;
|
|
@@ -99,25 +105,72 @@ async fn main() -> std::io::Result<()> {
|
99
|
105
|
Ok(res)
|
100
|
106
|
}));
|
101
|
107
|
|
102
|
|
- app.at("/").get(|_| async {
|
|
108
|
+ app.with(tide::sessions::SessionMiddleware::new(
|
|
109
|
+ tide::sessions::MemoryStore::new(),
|
|
110
|
+ std::env::var("TIDE_SECRET")
|
|
111
|
+ .expect(
|
|
112
|
+ "Please provide a TIDE_SECRET value of at \
|
|
113
|
+ least 32 bytes in order to run this example",
|
|
114
|
+ )
|
|
115
|
+ .as_bytes(),
|
|
116
|
+ ));
|
|
117
|
+
|
|
118
|
+ app.at("/").get(|req: tide::Request<()>| async move {
|
103
|
119
|
let tera = Tera::new("templates/**/*.html")?;
|
104
|
120
|
let posts = read_all_posts().await?;
|
105
|
121
|
let mut context = Context::new();
|
106
|
122
|
context.insert("posts", &posts);
|
|
123
|
+ let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
|
|
124
|
+ context.insert("logged_in", &logged_in);
|
107
|
125
|
let html = tera.render("index.html", &context)?;
|
108
|
126
|
Ok(Body::from_string(html))
|
109
|
127
|
});
|
110
|
128
|
|
111
|
129
|
app.at("/posts")
|
112
|
130
|
.post(|mut req: tide::Request<()>| async move {
|
113
|
|
- let mut post: Post = req.body_form().await?;
|
114
|
|
- post.id = Uuid::new_v4().to_string();
|
115
|
|
- post.date = Local::now().date().naive_local().to_string();
|
116
|
|
- post.body = post.body.trim().to_owned();
|
117
|
|
- post.save()?;
|
118
|
|
- Ok(tide::Redirect::new("/"))
|
|
131
|
+ let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
|
|
132
|
+ if !logged_in {
|
|
133
|
+ Ok(tide::Redirect::new("/login"))
|
|
134
|
+ } else {
|
|
135
|
+ let mut post: Post = req.body_form().await?;
|
|
136
|
+ post.id = Uuid::new_v4().to_string();
|
|
137
|
+ post.date = Local::now().date().naive_local().to_string();
|
|
138
|
+ post.body = post.body.trim().to_owned();
|
|
139
|
+ post.save()?;
|
|
140
|
+ Ok(tide::Redirect::new("/"))
|
|
141
|
+ }
|
119
|
142
|
});
|
120
|
143
|
|
|
144
|
+ app.at("/login").get(|req: tide::Request<()>| async move {
|
|
145
|
+ let tera = Tera::new("templates/**/*.html")?;
|
|
146
|
+ let mut context = Context::new();
|
|
147
|
+ let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
|
|
148
|
+ context.insert("logged_in", &logged_in);
|
|
149
|
+ let html = tera.render("login.html", &context)?;
|
|
150
|
+ Ok(Body::from_string(html))
|
|
151
|
+ }).post(|mut req: tide::Request<()>| async move {
|
|
152
|
+ let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
|
|
153
|
+ if logged_in {
|
|
154
|
+ return Ok(tide::Redirect::new("/"));
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ let username = env::var("ADMIN_USERNAME")?;
|
|
158
|
+ let password = env::var("ADMIN_PASSWORD")?;
|
|
159
|
+ let user: User = req.body_form().await?;
|
|
160
|
+ if user.username == username && user.password == password {
|
|
161
|
+ req.session_mut().insert("logged_in", true)?;
|
|
162
|
+ Ok(tide::Redirect::new("/"))
|
|
163
|
+ } else {
|
|
164
|
+ Ok(tide::Redirect::new("/login"))
|
|
165
|
+ }
|
|
166
|
+ });
|
|
167
|
+
|
|
168
|
+ app.at("/logout").get(|mut req: tide::Request<()>| async move {
|
|
169
|
+ req.session_mut().remove("logged_in");
|
|
170
|
+ req.session_mut().insert("logged_in", false)?;
|
|
171
|
+ Ok(tide::Redirect::new("/"))
|
|
172
|
+ });
|
|
173
|
+
|
121
|
174
|
app.listen("127.0.0.1:8080").await?;
|
122
|
175
|
|
123
|
176
|
Ok(())
|