Browse Source

Reorganize into separate library crate

This will make it easier to test
master
Dylan Baker 3 years ago
parent
commit
53355568f7

+ 1
- 0
.gitignore View File

@@ -1,2 +1,3 @@
1 1
 /target
2
+/microblog-lib/target
2 3
 .env

+ 16
- 0
Cargo.lock View File

@@ -931,6 +931,22 @@ checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
931 931
 [[package]]
932 932
 name = "microblog"
933 933
 version = "0.1.0"
934
+dependencies = [
935
+ "async-std",
936
+ "chrono",
937
+ "dotenv",
938
+ "microblog-lib",
939
+ "serde 1.0.115",
940
+ "serde_json",
941
+ "tera",
942
+ "tide 0.13.0",
943
+ "tide-tera",
944
+ "uuid",
945
+]
946
+
947
+[[package]]
948
+name = "microblog-lib"
949
+version = "0.1.0"
934 950
 dependencies = [
935 951
  "async-std",
936 952
  "chrono",

+ 4
- 1
Cargo.toml View File

@@ -6,13 +6,16 @@ edition = "2018"
6 6
 
7 7
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8 8
 
9
+[workspace]
10
+
9 11
 [dependencies]
10 12
 async-std = { version = "1.6.0", features = ["attributes"] }
11 13
 chrono = { version = "0.4", features = ["serde", "rustc-serialize"] }
12 14
 dotenv = "0.15.0"
13 15
 serde = "1.0.115"
16
+serde_json = "1.0.57"
14 17
 tera = "1.5.0"
15 18
 tide = "0.13.0"
16 19
 tide-tera = "0.1.1"
17 20
 uuid = { version = "0.4", features = ["serde", "v4"] }
18
-serde_json = "1.0.57"
21
+microblog-lib = { path = "microblog-lib" }

+ 2011
- 0
microblog-lib/Cargo.lock
File diff suppressed because it is too large
View File


+ 18
- 0
microblog-lib/Cargo.toml View File

@@ -0,0 +1,18 @@
1
+[package]
2
+name = "microblog-lib"
3
+version = "0.1.0"
4
+authors = ["Dylan Baker <dylan@simulacrum.party>"]
5
+edition = "2018"
6
+
7
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
+
9
+[dependencies]
10
+async-std = { version = "1.6.0", features = ["attributes"] }
11
+chrono = { version = "0.4", features = ["serde", "rustc-serialize"] }
12
+dotenv = "0.15.0"
13
+serde = "1.0.115"
14
+serde_json = "1.0.57"
15
+tera = "1.5.0"
16
+tide = "0.13.0"
17
+tide-tera = "0.1.1"
18
+uuid = { version = "0.4", features = ["serde", "v4"] }

src/fs.rs → microblog-lib/src/fs.rs View File


+ 47
- 0
microblog-lib/src/lib.rs View File

@@ -0,0 +1,47 @@
1
+use dotenv;
2
+use tide::utils::After;
3
+
4
+mod fs;
5
+mod middleware;
6
+mod post;
7
+mod routes;
8
+
9
+use middleware::*;
10
+
11
+#[derive(Clone)]
12
+pub struct State {
13
+    pub login_path: String,
14
+}
15
+
16
+pub async fn build_app() -> Result<tide::Server<State>, tide::Error> {
17
+    dotenv::dotenv().ok();
18
+
19
+    let login_path = std::env::var("LOGIN_PATH").unwrap_or(String::from("/login"));
20
+    let mut app = tide::with_state(State {
21
+        login_path: login_path.clone(),
22
+    });
23
+
24
+    app.at("/static").serve_dir("static")?;
25
+
26
+    app.with(After(errors));
27
+    app.with(session());
28
+
29
+    app.at("/").get(routes::index);
30
+    app.at("/posts")
31
+        .with(require_auth)
32
+        .post(routes::create_post);
33
+    app.at("/posts/:id")
34
+        .get(routes::single_post)
35
+        .post(routes::update_post)
36
+        .delete(routes::delete_post);
37
+    app.at("/posts/:id/edit")
38
+        .with(require_auth)
39
+        .get(routes::edit_post);
40
+    app.at(&login_path)
41
+        .with(require_guest)
42
+        .get(routes::login_page)
43
+        .post(routes::login);
44
+    app.at("/logout").with(require_auth).post(routes::logout);
45
+
46
+    Ok(app)
47
+}

src/middleware.rs → microblog-lib/src/middleware.rs View File


src/post.rs → microblog-lib/src/post.rs View File


src/routes.rs → microblog-lib/src/routes.rs View File

@@ -52,14 +52,16 @@ pub async fn create_post(mut req: Request<State>) -> Result {
52 52
 pub async fn update_post(mut req: Request<State>) -> Result {
53 53
     let mut post: Post = req.body_form().await?;
54 54
     post.save().await?;
55
-    req.session_mut().insert("flash_success", String::from("Post updated successfully"))?;
55
+    req.session_mut()
56
+        .insert("flash_success", String::from("Post updated successfully"))?;
56 57
     redirect(&format!("/posts/{}", post.id))
57 58
 }
58 59
 
59 60
 pub async fn delete_post(mut req: Request<State>) -> Result {
60 61
     let id: String = req.param("id")?;
61 62
     fs::delete_post(id)?;
62
-    req.session_mut().insert("flash_success", String::from("Post deleted successfully"))?;
63
+    req.session_mut()
64
+        .insert("flash_success", String::from("Post deleted successfully"))?;
63 65
     render_json_response(json!({ "success": "true" }))
64 66
 }
65 67
 
@@ -129,7 +131,6 @@ pub fn render_template(template: &str, context: &Context) -> Result<String> {
129 131
     Ok(html)
130 132
 }
131 133
 
132
-
133 134
 fn prepare_flash_messages(mut req: Request<State>) -> Context {
134 135
     let mut context = Context::new();
135 136
     context.insert("flash_error", &false);

+ 4
- 44
src/main.rs View File

@@ -1,52 +1,12 @@
1
-use dotenv;
2
-
3
-use middleware::*;
4
-use tide::utils::After;
5
-
6
-mod fs;
7
-mod middleware;
8
-mod post;
9
-mod routes;
10
-
11
-#[derive(Clone)]
12
-pub struct State {
13
-    pub login_path: String,
14
-}
1
+use microblog_lib::build_app;
2
+use tide::Result;
15 3
 
16 4
 #[async_std::main]
17
-async fn main() -> std::io::Result<()> {
18
-    dotenv::dotenv().ok();
5
+async fn main() -> Result<()> {
19 6
     tide::log::start();
20
-
21
-    let login_path = std::env::var("LOGIN_PATH").unwrap_or(String::from("/login"));
22 7
     let hostname = std::env::var("HOSTNAME").unwrap_or(String::from("127.0.0.1"));
23 8
     let port = std::env::var("PORT").unwrap_or(String::from("8080"));
24
-    let mut app = tide::with_state(State {
25
-        login_path: login_path.clone(),
26
-    });
27
-
28
-    app.at("/static").serve_dir("static")?;
29
-
30
-    app.with(After(errors));
31
-    app.with(session());
32
-
33
-    app.at("/").get(routes::index);
34
-    app.at("/posts")
35
-        .with(require_auth)
36
-        .post(routes::create_post);
37
-    app.at("/posts/:id")
38
-        .get(routes::single_post)
39
-        .post(routes::update_post)
40
-        .delete(routes::delete_post);
41
-    app.at("/posts/:id/edit")
42
-        .with(require_auth)
43
-        .get(routes::edit_post);
44
-    app.at(&login_path)
45
-        .with(require_guest)
46
-        .get(routes::login_page)
47
-        .post(routes::login);
48
-    app.at("/logout").with(require_auth).post(routes::logout);
49
-
9
+    let app = build_app().await?;
50 10
     app.listen(format!("{}:{}", hostname, port)).await?;
51 11
 
52 12
     Ok(())

Loading…
Cancel
Save