Skip to content

Commit c23ac8d

Browse files
Merge pull request #5 from hubertshelley/develop
Publish v0.4.0
2 parents e190be6 + 2efcdbb commit c23ac8d

File tree

25 files changed

+245
-118
lines changed

25 files changed

+245
-118
lines changed

Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
[workspace]
2-
members = ["silent", "examples/*"]
2+
members = ["silent*", "examples/*"]
33
default-members = ["silent", ]
4+
5+
[workspace.package]
6+
edition = "2021"
7+
authors = ["Hubert Shelley <hubertshelley@163.com>"]
8+
homepage = "https://github.com/hubertshelley/silent"
9+
license = "Apache-2.0"
10+
readme = "./readme.md"
11+
repository = "https://github.com/hubertshelley/silent"
12+
version = "0.4.0"

examples/file_server/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
[package]
2-
name = "file_server"
2+
name = "example-file_server"
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]
99
silent = { path = "../../silent" }
10-
async-trait = "0.1.68"
11-
tokio = { version = "1", features = ["full"] }

examples/file_server/src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ fn main() {
2424
)
2525
.unwrap();
2626
}
27-
let mut route = Route::new("<path:**>");
28-
route
29-
.get_handler_mut()
30-
.insert(Method::GET, Arc::new(static_handler("static")));
27+
let route =
28+
Route::new("<path:**>").insert_handler(Method::GET, Arc::new(static_handler("static")));
3129
Server::new().bind_route(route).run();
3230
}

examples/form/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ struct Input {
1414
email: String,
1515
}
1616

17-
async fn accept_form(mut req: Request) -> Result<Option<Input>, SilentError> {
17+
async fn accept_form(mut req: Request) -> Result<Option<Input>> {
1818
req.json_parse().await
1919
}
2020

21-
async fn show_form(_req: Request) -> Result<&'static str, SilentError> {
21+
async fn show_form(_req: Request) -> Result<&'static str> {
2222
Ok(r#"
2323
<!doctype html>
2424
<html>

examples/middleware/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-middleware"
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+
async-trait = "0.1.68"
10+
silent = { path = "../../silent" }

examples/middleware/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

examples/multipart-form/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
Server::new().bind_route(route).run();
88
}
99

10-
async fn show_form(_req: Request) -> Result<&'static str, SilentError> {
10+
async fn show_form(_req: Request) -> Result<&'static str> {
1111
Ok(r#"
1212
<!doctype html>
1313
<html>
@@ -32,7 +32,7 @@ struct File {
3232
file_name: String,
3333
}
3434

35-
async fn accept_form(mut req: Request) -> Result<Vec<File>, SilentError> {
35+
async fn accept_form(mut req: Request) -> Result<Vec<File>> {
3636
let mut result_files = vec![];
3737
if let Some(files) = req.files("files").await {
3838
for file in files {

examples/path_params/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ fn main() {
1818
}
1919

2020
// 定义处理方法
21-
async fn hello_world(req: Request) -> Result<String, SilentError> {
22-
let path_params = req.get_path_params("key").unwrap();
21+
async fn hello_world(req: Request) -> Result<String> {
22+
let path_params = req.path_params.get("key").unwrap();
2323
match path_params {
2424
PathParam::String(str) => Ok(format!("str {}", str)),
2525
PathParam::Int(int) => Ok(format!("int {}", int)),
26-
PathParam::UUid(uuid) => Ok(format!("uuid {}", uuid)),
26+
PathParam::Uuid(uuid) => Ok(format!("uuid {}", uuid)),
2727
PathParam::Path(path) => Ok(format!("path {}", path)),
2828
}
2929
}

examples/todo/src/main.rs

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515
.post(todos_create)
1616
.append(
1717
Route::new("<id:uuid>")
18+
.get(todos_one)
1819
.patch(todos_update)
1920
.delete(todos_delete),
2021
);
@@ -26,12 +27,8 @@ struct MiddleWare {
2627
}
2728

2829
#[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> {
30+
impl MiddleWareHandler for MiddleWare {
31+
async fn pre_request(&self, req: &mut Request, _res: &mut Response) -> Result<()> {
3532
req.extensions_mut().insert(self.db.clone());
3633
Ok(())
3734
}
@@ -43,7 +40,7 @@ pub struct Pagination {
4340
pub limit: Option<usize>,
4441
}
4542

46-
async fn todos_index(mut req: Request) -> Result<Vec<Todo>, SilentError> {
43+
async fn todos_index(mut req: Request) -> Result<Vec<Todo>> {
4744
let pagination = req.params_parse::<Pagination>()?;
4845

4946
let db = req.extensions().get::<Db>().unwrap();
@@ -64,7 +61,7 @@ struct CreateTodo {
6461
text: String,
6562
}
6663

67-
async fn todos_create(mut req: Request) -> Result<Todo, SilentError> {
64+
async fn todos_create(mut req: Request) -> Result<Todo> {
6865
let create_todo = req.json_parse::<CreateTodo>().await?;
6966
let db = req.extensions().get::<Db>().unwrap();
7067

@@ -85,53 +82,55 @@ struct UpdateTodo {
8582
completed: Option<bool>,
8683
}
8784

88-
async fn todos_update(mut req: Request) -> Result<Todo, SilentError> {
85+
async fn todos_update(mut req: Request) -> Result<Todo> {
8986
let input = req.json_parse::<UpdateTodo>().await?;
9087
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();
88+
let id: Uuid = req.get_path_params("id")?;
89+
let todo = db.read().unwrap().get(&id).cloned();
9490

95-
if todo.is_none() {
96-
return Err(SilentError::BusinessError {
97-
code: StatusCode::NOT_FOUND,
98-
msg: "Not Found".to_string(),
99-
});
100-
}
91+
if todo.is_none() {
92+
return Err(SilentError::BusinessError {
93+
code: StatusCode::NOT_FOUND,
94+
msg: "Not Found".to_string(),
95+
});
96+
}
10197

102-
let mut todo = todo.unwrap();
98+
let mut todo = todo.unwrap();
10399

104-
if let Some(text) = input.text {
105-
todo.text = text;
106-
}
100+
if let Some(text) = input.text {
101+
todo.text = text;
102+
}
107103

108-
if let Some(completed) = input.completed {
109-
todo.completed = completed;
110-
}
104+
if let Some(completed) = input.completed {
105+
todo.completed = completed;
106+
}
111107

112-
db.write().unwrap().insert(todo.id, todo.clone());
108+
db.write().unwrap().insert(todo.id, todo.clone());
113109

114-
Ok(todo)
115-
} else {
116-
Err(SilentError::BusinessError {
110+
Ok(todo)
111+
}
112+
113+
async fn todos_one(req: Request) -> Result<Todo> {
114+
let db = req.extensions().get::<Db>().unwrap();
115+
let id: Uuid = req.get_path_params("id")?;
116+
let todo = db.read().unwrap().get(&id).cloned();
117+
118+
if todo.is_none() {
119+
return Err(SilentError::BusinessError {
117120
code: StatusCode::NOT_FOUND,
118121
msg: "Not Found".to_string(),
119-
})
122+
});
120123
}
124+
125+
let todo = todo.unwrap();
126+
Ok(todo)
121127
}
122128

123-
async fn todos_delete(req: Request) -> Result<(), SilentError> {
129+
async fn todos_delete(req: Request) -> Result<()> {
124130
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-
}
131+
let id = req.get_path_params("id")?;
132+
if db.write().unwrap().remove(&id).is_some() {
133+
Ok(())
135134
} else {
136135
Err(SilentError::BusinessError {
137136
code: StatusCode::NOT_FOUND,

silent/Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
[package]
22
name = "silent"
3-
edition = "2021"
4-
authors = ["Hubert Shelley <hubertshelley@163.com>"]
3+
edition.workspace = true
4+
authors.workspace = true
55
categories = ["web-programming::web", "web-programming::web-framework"]
6-
documentation = "https://docs.rs/"
6+
documentation = "https://docs.rs/silent/"
77
description = """
88
Silent Web Framework
99
"""
10-
homepage = "https://github.com/hubertshelley/silent"
10+
homepage.workspace = true
1111
keywords = ["web", "web-framework"]
12-
license = "Apache-2.0"
13-
readme = "../readme.md"
14-
repository = "https://github.com/hubertshelley/silent"
15-
version = "0.3.0"
12+
license.workspace = true
13+
readme.workspace = true
14+
repository.workspace = true
15+
version.workspace = true
1616
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1717

1818
[dependencies]

0 commit comments

Comments
 (0)