-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from hubertshelley/develop
v0.2.1 publish
- Loading branch information
Showing
22 changed files
with
1,173 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "example-form" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
silent = { path = "../../silent" } | ||
serde = { version = "1.0.160", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use silent::prelude::*; | ||
|
||
fn main() { | ||
logger::fmt().init(); | ||
let route = Route::new("").get_html(show_form).post(accept_form); | ||
Server::new().bind_route(route).run(); | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
#[allow(dead_code)] | ||
struct Input { | ||
name: String, | ||
email: String, | ||
} | ||
|
||
async fn accept_form(mut req: Request) -> Result<Option<Input>, SilentError> { | ||
req.json_parse().await | ||
} | ||
|
||
async fn show_form(_req: Request) -> Result<&'static str, SilentError> { | ||
Ok(r#" | ||
<!doctype html> | ||
<html> | ||
<head></head> | ||
<body> | ||
<form action="/" method="post"> | ||
<label for="name"> | ||
Enter your name: | ||
<input type="text" name="name"> | ||
</label> | ||
<label> | ||
Enter your email: | ||
<input type="text" name="email"> | ||
</label> | ||
<input type="submit" value="Subscribe!"> | ||
</form> | ||
</body> | ||
</html> | ||
"#) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "example-multipart-form" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
silent = { path = "../../silent" } | ||
serde = { version = "1.0.160", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use silent::prelude::*; | ||
|
||
fn main() { | ||
logger::fmt().init(); | ||
let route = Route::new("").get_html(show_form).post(accept_form); | ||
Server::new().bind_route(route).run(); | ||
} | ||
|
||
async fn show_form(_req: Request) -> Result<&'static str, SilentError> { | ||
Ok(r#" | ||
<!doctype html> | ||
<html> | ||
<head></head> | ||
<body> | ||
<form action="" method="post" enctype="multipart/form-data"> | ||
<label> | ||
Upload file: | ||
<input type="file" name="files" multiple> | ||
</label> | ||
<input type="submit" value="Upload files"> | ||
</form> | ||
</body> | ||
</html> | ||
"#) | ||
} | ||
|
||
#[derive(Debug, Deserialize, Serialize, Default)] | ||
struct File { | ||
name: String, | ||
file_name: String, | ||
} | ||
|
||
async fn accept_form(mut req: Request) -> Result<Vec<File>, SilentError> { | ||
let mut result_files = vec![]; | ||
if let Some(files) = req.files("files").await { | ||
for file in files { | ||
result_files.push(File { | ||
name: file.name().unwrap_or("file").to_string(), | ||
file_name: file.path().to_string_lossy().to_string(), | ||
}); | ||
} | ||
} | ||
Ok(result_files) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
[package] | ||
name = "path_params" | ||
name = "example-path_params" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
silent = { version = "0.1.0", path = "../../silent" } | ||
silent = { path = "../../silent" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "example-todo" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
silent = { path = "../../silent" } | ||
serde = { version = "1.0.160", features = ["derive"] } | ||
uuid = { version = "1.3.2", features = ["serde", "v4"] } | ||
async-trait = "0.1.68" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
use async_trait::async_trait; | ||
use serde::{Deserialize, Serialize}; | ||
use silent::prelude::*; | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, RwLock}; | ||
use uuid::Uuid; | ||
|
||
fn main() { | ||
logger::fmt().init(); | ||
let db = Db::default(); | ||
let middle_ware = MiddleWare { db }; | ||
let route = Route::new("todos") | ||
.hook(middle_ware) | ||
.get(todos_index) | ||
.post(todos_create) | ||
.append( | ||
Route::new("<id:uuid>") | ||
.patch(todos_update) | ||
.delete(todos_delete), | ||
); | ||
Server::new().bind_route(route).run(); | ||
} | ||
|
||
struct MiddleWare { | ||
db: Db, | ||
} | ||
|
||
#[async_trait] | ||
impl Handler for MiddleWare { | ||
async fn middleware_call( | ||
&self, | ||
req: &mut Request, | ||
_res: &mut Response, | ||
) -> Result<(), SilentError> { | ||
req.extensions_mut().insert(self.db.clone()); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Deserialize, Default)] | ||
pub struct Pagination { | ||
pub offset: Option<usize>, | ||
pub limit: Option<usize>, | ||
} | ||
|
||
async fn todos_index(mut req: Request) -> Result<Vec<Todo>, SilentError> { | ||
let pagination = req.params_parse::<Pagination>()?; | ||
|
||
let db = req.extensions().get::<Db>().unwrap(); | ||
let todos = db.read().unwrap(); | ||
|
||
let todos = todos | ||
.values() | ||
.skip(pagination.offset.unwrap_or(0)) | ||
.take(pagination.limit.unwrap_or(usize::MAX)) | ||
.cloned() | ||
.collect::<Vec<_>>(); | ||
|
||
Ok(todos) | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct CreateTodo { | ||
text: String, | ||
} | ||
|
||
async fn todos_create(mut req: Request) -> Result<Todo, SilentError> { | ||
let create_todo = req.json_parse::<CreateTodo>().await?; | ||
let db = req.extensions().get::<Db>().unwrap(); | ||
|
||
let todo = Todo { | ||
id: Uuid::new_v4(), | ||
text: create_todo.text, | ||
completed: false, | ||
}; | ||
|
||
db.write().unwrap().insert(todo.id, todo.clone()); | ||
|
||
Ok(todo) | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct UpdateTodo { | ||
text: Option<String>, | ||
completed: Option<bool>, | ||
} | ||
|
||
async fn todos_update(mut req: Request) -> Result<Todo, SilentError> { | ||
let input = req.json_parse::<UpdateTodo>().await?; | ||
let db = req.extensions().get::<Db>().unwrap(); | ||
let id = req.get_path_params("id").unwrap(); | ||
if let PathParam::UUid(id) = id { | ||
let todo = db.read().unwrap().get(id).cloned(); | ||
|
||
if todo.is_none() { | ||
return Err(SilentError::BusinessError { | ||
code: StatusCode::NOT_FOUND, | ||
msg: "Not Found".to_string(), | ||
}); | ||
} | ||
|
||
let mut todo = todo.unwrap(); | ||
|
||
if let Some(text) = input.text { | ||
todo.text = text; | ||
} | ||
|
||
if let Some(completed) = input.completed { | ||
todo.completed = completed; | ||
} | ||
|
||
db.write().unwrap().insert(todo.id, todo.clone()); | ||
|
||
Ok(todo) | ||
} else { | ||
Err(SilentError::BusinessError { | ||
code: StatusCode::NOT_FOUND, | ||
msg: "Not Found".to_string(), | ||
}) | ||
} | ||
} | ||
|
||
async fn todos_delete(req: Request) -> Result<(), SilentError> { | ||
let db = req.extensions().get::<Db>().unwrap(); | ||
let id = req.get_path_params("id").unwrap(); | ||
if let PathParam::UUid(id) = id { | ||
if db.write().unwrap().remove(id).is_some() { | ||
Ok(()) | ||
} else { | ||
Err(SilentError::BusinessError { | ||
code: StatusCode::NOT_FOUND, | ||
msg: "Not Found".to_string(), | ||
}) | ||
} | ||
} else { | ||
Err(SilentError::BusinessError { | ||
code: StatusCode::NOT_FOUND, | ||
msg: "Not Found".to_string(), | ||
}) | ||
} | ||
} | ||
|
||
type Db = Arc<RwLock<HashMap<Uuid, Todo>>>; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
struct Todo { | ||
id: Uuid, | ||
text: String, | ||
completed: bool, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.