mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-21 14:46:32 +01:00
Update mongodb example (#2944)
This commit is contained in:
parent
52fd139a86
commit
3df15fd2ea
2 changed files with 10 additions and 9 deletions
|
@ -6,9 +6,9 @@ publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = { path = "../../axum" }
|
axum = { path = "../../axum" }
|
||||||
mongodb = "2.8.0"
|
mongodb = "3.1.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
tokio = { version = "1.0", features = ["full"] }
|
tokio = { version = "1.0", features = ["full"] }
|
||||||
tower-http = { version = "0.5.0", features = ["add-extension", "trace"] }
|
tower-http = { version = "0.6.1", features = ["add-extension", "trace"] }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
|
|
|
@ -30,7 +30,7 @@ async fn main() {
|
||||||
// pinging the database
|
// pinging the database
|
||||||
client
|
client
|
||||||
.database("axum-mongo")
|
.database("axum-mongo")
|
||||||
.run_command(doc! { "ping": 1 }, None)
|
.run_command(doc! { "ping": 1 })
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
println!("Pinged your database. Successfully connected to MongoDB!");
|
println!("Pinged your database. Successfully connected to MongoDB!");
|
||||||
|
@ -38,8 +38,9 @@ async fn main() {
|
||||||
// logging middleware
|
// logging middleware
|
||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
.with(
|
.with(
|
||||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
||||||
.unwrap_or_else(|_| "example_mongo=debug,tower_http=debug".into()),
|
format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
.with(tracing_subscriber::fmt::layer())
|
.with(tracing_subscriber::fmt::layer())
|
||||||
.init();
|
.init();
|
||||||
|
@ -70,7 +71,7 @@ async fn create_member(
|
||||||
State(db): State<Collection<Member>>,
|
State(db): State<Collection<Member>>,
|
||||||
Json(input): Json<Member>,
|
Json(input): Json<Member>,
|
||||||
) -> Result<Json<InsertOneResult>, (StatusCode, String)> {
|
) -> Result<Json<InsertOneResult>, (StatusCode, String)> {
|
||||||
let result = db.insert_one(input, None).await.map_err(internal_error)?;
|
let result = db.insert_one(input).await.map_err(internal_error)?;
|
||||||
|
|
||||||
Ok(Json(result))
|
Ok(Json(result))
|
||||||
}
|
}
|
||||||
|
@ -81,7 +82,7 @@ async fn read_member(
|
||||||
Path(id): Path<u32>,
|
Path(id): Path<u32>,
|
||||||
) -> Result<Json<Option<Member>>, (StatusCode, String)> {
|
) -> Result<Json<Option<Member>>, (StatusCode, String)> {
|
||||||
let result = db
|
let result = db
|
||||||
.find_one(doc! { "_id": id }, None)
|
.find_one(doc! { "_id": id })
|
||||||
.await
|
.await
|
||||||
.map_err(internal_error)?;
|
.map_err(internal_error)?;
|
||||||
|
|
||||||
|
@ -94,7 +95,7 @@ async fn update_member(
|
||||||
Json(input): Json<Member>,
|
Json(input): Json<Member>,
|
||||||
) -> Result<Json<UpdateResult>, (StatusCode, String)> {
|
) -> Result<Json<UpdateResult>, (StatusCode, String)> {
|
||||||
let result = db
|
let result = db
|
||||||
.replace_one(doc! { "_id": input.id }, input, None)
|
.replace_one(doc! { "_id": input.id }, input)
|
||||||
.await
|
.await
|
||||||
.map_err(internal_error)?;
|
.map_err(internal_error)?;
|
||||||
|
|
||||||
|
@ -107,7 +108,7 @@ async fn delete_member(
|
||||||
Path(id): Path<u32>,
|
Path(id): Path<u32>,
|
||||||
) -> Result<Json<DeleteResult>, (StatusCode, String)> {
|
) -> Result<Json<DeleteResult>, (StatusCode, String)> {
|
||||||
let result = db
|
let result = db
|
||||||
.delete_one(doc! { "_id": id }, None)
|
.delete_one(doc! { "_id": id })
|
||||||
.await
|
.await
|
||||||
.map_err(internal_error)?;
|
.map_err(internal_error)?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue