|
@@ -6,11 +6,10 @@ use std::path::PathBuf;
|
6
|
6
|
use dotenv;
|
7
|
7
|
use serde::{Deserialize, Serialize};
|
8
|
8
|
use serde_json;
|
9
|
|
-use tera::Context;
|
10
|
9
|
use tide::utils::After;
|
11
|
|
-use tide::{Response, StatusCode};
|
12
|
10
|
|
13
|
11
|
mod fs;
|
|
12
|
+mod middleware;
|
14
|
13
|
mod routes;
|
15
|
14
|
|
16
|
15
|
#[derive(Debug, Serialize, Deserialize)]
|
|
@@ -50,46 +49,8 @@ async fn main() -> std::io::Result<()> {
|
50
|
49
|
tide::log::start();
|
51
|
50
|
let mut app = tide::new();
|
52
|
51
|
|
53
|
|
- app.with(After(|mut res: Response| async {
|
54
|
|
- let mut context = Context::new();
|
55
|
|
-
|
56
|
|
- match res.downcast_error::<async_std::io::Error>() {
|
57
|
|
- Some(e) => {
|
58
|
|
- context.insert("error", &e.to_string());
|
59
|
|
- let status = if let ErrorKind::NotFound = e.kind() {
|
60
|
|
- StatusCode::NotFound
|
61
|
|
- } else {
|
62
|
|
- StatusCode::InternalServerError
|
63
|
|
- };
|
64
|
|
- res.set_body(routes::render_template("error.html", &context)?);
|
65
|
|
- res.set_status(status);
|
66
|
|
- }
|
67
|
|
- None => match res.status() {
|
68
|
|
- StatusCode::NotFound => {
|
69
|
|
- context.insert("error", "Page not found");
|
70
|
|
- res.set_body(routes::render_template("error.html", &context)?);
|
71
|
|
- }
|
72
|
|
- _ => {}
|
73
|
|
- },
|
74
|
|
- };
|
75
|
|
-
|
76
|
|
- Ok(res)
|
77
|
|
- }));
|
78
|
|
-
|
79
|
|
- app.with(After(|mut res: Response| async {
|
80
|
|
- res.set_content_type(tide::http::mime::HTML);
|
81
|
|
- Ok(res)
|
82
|
|
- }));
|
83
|
|
-
|
84
|
|
- app.with(tide::sessions::SessionMiddleware::new(
|
85
|
|
- tide::sessions::MemoryStore::new(),
|
86
|
|
- std::env::var("TIDE_SECRET")
|
87
|
|
- .expect(
|
88
|
|
- "Please provide a TIDE_SECRET value of at \
|
89
|
|
- least 32 bytes in order to run this example",
|
90
|
|
- )
|
91
|
|
- .as_bytes(),
|
92
|
|
- ));
|
|
52
|
+ app.with(After(middleware::errors));
|
|
53
|
+ app.with(middleware::session());
|
93
|
54
|
|
94
|
55
|
app.at("/").get(routes::index);
|
95
|
56
|
app.at("/posts").post(routes::create_post);
|