Skip to content

Commit b15c3b1

Browse files
Merge pull request #3 from hubertshelley/develop
v0.2.1 publish
2 parents 20a5d4f + 9c9ad60 commit b15c3b1

File tree

22 files changed

+1173
-61
lines changed

22 files changed

+1173
-61
lines changed

deny.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ allow = [
7676
# "Apache-2.0 WITH LLVM-exception",
7777
# "ISC",
7878
"Unicode-DFS-2016",
79-
# "BSD-3-Clause",
79+
"BSD-3-Clause",
8080
# "BSD-2-Clause",
8181
]
8282
# List of explicitly disallowed licenses

examples/form/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "example-form"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
silent = { path = "../../silent" }
10+
serde = { version = "1.0.160", features = ["derive"] }

examples/form/src/main.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use serde::{Deserialize, Serialize};
2+
use silent::prelude::*;
3+
4+
fn main() {
5+
logger::fmt().init();
6+
let route = Route::new("").get_html(show_form).post(accept_form);
7+
Server::new().bind_route(route).run();
8+
}
9+
10+
#[derive(Deserialize, Serialize, Debug)]
11+
#[allow(dead_code)]
12+
struct Input {
13+
name: String,
14+
email: String,
15+
}
16+
17+
async fn accept_form(mut req: Request) -> Result<Option<Input>, SilentError> {
18+
req.json_parse().await
19+
}
20+
21+
async fn show_form(_req: Request) -> Result<&'static str, SilentError> {
22+
Ok(r#"
23+
<!doctype html>
24+
<html>
25+
<head></head>
26+
<body>
27+
<form action="/" method="post">
28+
<label for="name">
29+
Enter your name:
30+
<input type="text" name="name">
31+
</label>
32+
33+
<label>
34+
Enter your email:
35+
<input type="text" name="email">
36+
</label>
37+
38+
<input type="submit" value="Subscribe!">
39+
</form>
40+
</body>
41+
</html>
42+
"#)
43+
}

examples/hello-world/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
silent = { version = "0.1.0", path = "../../silent" }
9+
silent = { path = "../../silent" }

examples/multipart-form/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "example-multipart-form"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
silent = { path = "../../silent" }
10+
serde = { version = "1.0.160", features = ["derive"] }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use serde::{Deserialize, Serialize};
2+
use silent::prelude::*;
3+
4+
fn main() {
5+
logger::fmt().init();
6+
let route = Route::new("").get_html(show_form).post(accept_form);
7+
Server::new().bind_route(route).run();
8+
}
9+
10+
async fn show_form(_req: Request) -> Result<&'static str, SilentError> {
11+
Ok(r#"
12+
<!doctype html>
13+
<html>
14+
<head></head>
15+
<body>
16+
<form action="" method="post" enctype="multipart/form-data">
17+
<label>
18+
Upload file:
19+
<input type="file" name="files" multiple>
20+
</label>
21+
22+
<input type="submit" value="Upload files">
23+
</form>
24+
</body>
25+
</html>
26+
"#)
27+
}
28+
29+
#[derive(Debug, Deserialize, Serialize, Default)]
30+
struct File {
31+
name: String,
32+
file_name: String,
33+
}
34+
35+
async fn accept_form(mut req: Request) -> Result<Vec<File>, SilentError> {
36+
let mut result_files = vec![];
37+
if let Some(files) = req.files("files").await {
38+
for file in files {
39+
result_files.push(File {
40+
name: file.name().unwrap_or("file").to_string(),
41+
file_name: file.path().to_string_lossy().to_string(),
42+
});
43+
}
44+
}
45+
Ok(result_files)
46+
}

examples/path_params/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
2-
name = "path_params"
2+
name = "example-path_params"
33
version = "0.1.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
silent = { version = "0.1.0", path = "../../silent" }
9+
silent = { path = "../../silent" }

examples/todo/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "example-todo"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
silent = { path = "../../silent" }
10+
serde = { version = "1.0.160", features = ["derive"] }
11+
uuid = { version = "1.3.2", features = ["serde", "v4"] }
12+
async-trait = "0.1.68"

examples/todo/src/main.rs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
use async_trait::async_trait;
2+
use serde::{Deserialize, Serialize};
3+
use silent::prelude::*;
4+
use std::collections::HashMap;
5+
use std::sync::{Arc, RwLock};
6+
use uuid::Uuid;
7+
8+
fn main() {
9+
logger::fmt().init();
10+
let db = Db::default();
11+
let middle_ware = MiddleWare { db };
12+
let route = Route::new("todos")
13+
.hook(middle_ware)
14+
.get(todos_index)
15+
.post(todos_create)
16+
.append(
17+
Route::new("<id:uuid>")
18+
.patch(todos_update)
19+
.delete(todos_delete),
20+
);
21+
Server::new().bind_route(route).run();
22+
}
23+
24+
struct MiddleWare {
25+
db: Db,
26+
}
27+
28+
#[async_trait]
29+
impl Handler for MiddleWare {
30+
async fn middleware_call(
31+
&self,
32+
req: &mut Request,
33+
_res: &mut Response,
34+
) -> Result<(), SilentError> {
35+
req.extensions_mut().insert(self.db.clone());
36+
Ok(())
37+
}
38+
}
39+
40+
#[derive(Debug, Deserialize, Default)]
41+
pub struct Pagination {
42+
pub offset: Option<usize>,
43+
pub limit: Option<usize>,
44+
}
45+
46+
async fn todos_index(mut req: Request) -> Result<Vec<Todo>, SilentError> {
47+
let pagination = req.params_parse::<Pagination>()?;
48+
49+
let db = req.extensions().get::<Db>().unwrap();
50+
let todos = db.read().unwrap();
51+
52+
let todos = todos
53+
.values()
54+
.skip(pagination.offset.unwrap_or(0))
55+
.take(pagination.limit.unwrap_or(usize::MAX))
56+
.cloned()
57+
.collect::<Vec<_>>();
58+
59+
Ok(todos)
60+
}
61+
62+
#[derive(Debug, Deserialize)]
63+
struct CreateTodo {
64+
text: String,
65+
}
66+
67+
async fn todos_create(mut req: Request) -> Result<Todo, SilentError> {
68+
let create_todo = req.json_parse::<CreateTodo>().await?;
69+
let db = req.extensions().get::<Db>().unwrap();
70+
71+
let todo = Todo {
72+
id: Uuid::new_v4(),
73+
text: create_todo.text,
74+
completed: false,
75+
};
76+
77+
db.write().unwrap().insert(todo.id, todo.clone());
78+
79+
Ok(todo)
80+
}
81+
82+
#[derive(Debug, Deserialize)]
83+
struct UpdateTodo {
84+
text: Option<String>,
85+
completed: Option<bool>,
86+
}
87+
88+
async fn todos_update(mut req: Request) -> Result<Todo, SilentError> {
89+
let input = req.json_parse::<UpdateTodo>().await?;
90+
let db = req.extensions().get::<Db>().unwrap();
91+
let id = req.get_path_params("id").unwrap();
92+
if let PathParam::UUid(id) = id {
93+
let todo = db.read().unwrap().get(id).cloned();
94+
95+
if todo.is_none() {
96+
return Err(SilentError::BusinessError {
97+
code: StatusCode::NOT_FOUND,
98+
msg: "Not Found".to_string(),
99+
});
100+
}
101+
102+
let mut todo = todo.unwrap();
103+
104+
if let Some(text) = input.text {
105+
todo.text = text;
106+
}
107+
108+
if let Some(completed) = input.completed {
109+
todo.completed = completed;
110+
}
111+
112+
db.write().unwrap().insert(todo.id, todo.clone());
113+
114+
Ok(todo)
115+
} else {
116+
Err(SilentError::BusinessError {
117+
code: StatusCode::NOT_FOUND,
118+
msg: "Not Found".to_string(),
119+
})
120+
}
121+
}
122+
123+
async fn todos_delete(req: Request) -> Result<(), SilentError> {
124+
let db = req.extensions().get::<Db>().unwrap();
125+
let id = req.get_path_params("id").unwrap();
126+
if let PathParam::UUid(id) = id {
127+
if db.write().unwrap().remove(id).is_some() {
128+
Ok(())
129+
} else {
130+
Err(SilentError::BusinessError {
131+
code: StatusCode::NOT_FOUND,
132+
msg: "Not Found".to_string(),
133+
})
134+
}
135+
} else {
136+
Err(SilentError::BusinessError {
137+
code: StatusCode::NOT_FOUND,
138+
msg: "Not Found".to_string(),
139+
})
140+
}
141+
}
142+
143+
type Db = Arc<RwLock<HashMap<Uuid, Todo>>>;
144+
145+
#[derive(Debug, Serialize, Deserialize, Clone)]
146+
struct Todo {
147+
id: Uuid,
148+
text: String,
149+
completed: bool,
150+
}

silent/Cargo.toml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Silent Web Framework
1010
homepage = "https://github.com/hubertshelley/silent"
1111
keywords = ["web", "web-framework"]
1212
license = "Apache-2.0"
13-
readme = "./readme.md"
13+
readme = "../readme.md"
1414
repository = "https://github.com/hubertshelley/silent"
15-
version = "0.1.0"
15+
version = "0.2.1"
1616
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1717

1818
[dependencies]
@@ -21,10 +21,17 @@ hyper = { version = "1.0.0-rc.3", features = ["full"] }
2121
tokio = { version = "1", features = ["full"] }
2222
bytes = "1"
2323
http-body-util = "0.1.0-rc.2"
24-
#salvo_core = { git = "https://github.com/hubertshelley/salvo.git" }
25-
tracing = "0.1.37"
26-
tracing-subscriber = "0.3.17"
27-
async-trait = "0.1.68"
24+
tracing = "0.1"
25+
tracing-subscriber = "0.3"
26+
async-trait = "0.1"
2827
serde = { version = "1.0.160", features = ["derive"] }
29-
serde_json = "1.0.96"
30-
uuid = "1.3.2"
28+
serde_json = "1"
29+
uuid = "1"
30+
url = "2"
31+
serde_urlencoded = "0.7"
32+
multimap = { version = "0.9", features = ["serde"] }
33+
mime = "0.3"
34+
tempfile = "3"
35+
textnonce = "1"
36+
multer = "2"
37+
futures-util = { version = "0.3", features = ["io"] }

0 commit comments

Comments
 (0)