Skip to content

Commit 926245e

Browse files
Merge pull request LuceCarter#1 from abr-egn/master
Update tokio/async-std bridging in tide example
2 parents 323449e + a18fcad commit 926245e

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pub enum AppError {
1111
#[error("Serialization error: {0}")]
1212
Serialization(#[from] serde_json::Error),
1313

14+
#[error("Handler error: {0}")]
15+
HandlerError(#[from] tokio::task::JoinError),
16+
1417
#[error("Not found")]
1518
NotFound,
1619

src/frameworks/tide.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use mongodb::Database;
33
use bson::oid::ObjectId;
44
use serde_json::Value;
55
use std::sync::Arc;
6-
use tokio::runtime::Runtime;
6+
use tokio::runtime::Handle;
77

88
use crate::{
99
db::mongodb::MongoRepo,
@@ -14,12 +14,12 @@ use crate::{
1414
#[derive(Clone)]
1515
struct State {
1616
repo: Arc<MongoRepo>,
17-
runtime: Arc<Runtime>,
17+
runtime: Handle,
1818
}
1919

2020
pub async fn start(db: Database) -> Result<(), Box<dyn std::error::Error>> {
21-
// Create a Tokio runtime for MongoDB operations
22-
let runtime = Arc::new(Runtime::new()?);
21+
// Get a handle to the Tokio runtime for MongoDB operations
22+
let runtime = Handle::current();
2323

2424
let state = State {
2525
repo: Arc::new(MongoRepo::new(&db)),
@@ -49,9 +49,10 @@ async fn create_restaurant(mut req: Request<State>) -> tide::Result {
4949
let repo = req.state().repo.clone();
5050
let runtime = req.state().runtime.clone();
5151

52-
let result = runtime.block_on(async move {
53-
repo.create_restaurant(restaurant).await
54-
});
52+
let result = runtime
53+
.spawn(async move { repo.create_restaurant(restaurant).await })
54+
.await
55+
.unwrap_or_else(|e| Err(AppError::from(e)));
5556

5657
match result {
5758
Ok(created) => Ok(Response::builder(StatusCode::Created)
@@ -67,9 +68,10 @@ async fn list_restaurants(req: Request<State>) -> tide::Result {
6768
let repo = req.state().repo.clone();
6869
let runtime = req.state().runtime.clone();
6970

70-
let result = runtime.block_on(async move {
71-
repo.get_restaurants(10).await
72-
});
71+
let result = runtime
72+
.spawn(async move { repo.get_restaurants(10).await })
73+
.await
74+
.unwrap_or_else(|e| Err(AppError::from(e)));
7375

7476
match result {
7577
Ok(restaurants) => Ok(Response::builder(StatusCode::Ok)
@@ -93,9 +95,10 @@ async fn get_restaurant(req: Request<State>) -> tide::Result {
9395
let repo = req.state().repo.clone();
9496
let runtime = req.state().runtime.clone();
9597

96-
let result = runtime.block_on(async move {
97-
repo.get_restaurant_by_id(object_id).await
98-
});
98+
let result = runtime
99+
.spawn(async move { repo.get_restaurant_by_id(object_id).await })
100+
.await
101+
.unwrap_or_else(|e| Err(AppError::from(e)));
99102

100103
match result {
101104
Ok(restaurant) => Ok(Response::builder(StatusCode::Ok)
@@ -130,9 +133,10 @@ async fn update_restaurant(mut req: Request<State>) -> tide::Result {
130133
let repo = req.state().repo.clone();
131134
let runtime = req.state().runtime.clone();
132135

133-
let result = runtime.block_on(async move {
134-
repo.update_restaurant(object_id, update_doc).await
135-
});
136+
let result = runtime
137+
.spawn(async move { repo.update_restaurant(object_id, update_doc).await })
138+
.await
139+
.unwrap_or_else(|e| Err(AppError::from(e)));
136140

137141
match result {
138142
Ok(updated) => Ok(Response::builder(StatusCode::Ok)
@@ -159,9 +163,10 @@ async fn delete_restaurant(req: Request<State>) -> tide::Result {
159163
let repo = req.state().repo.clone();
160164
let runtime = req.state().runtime.clone();
161165

162-
let result = runtime.block_on(async move {
163-
repo.delete_restaurant(object_id).await
164-
});
166+
let result = runtime
167+
.spawn(async move { repo.delete_restaurant(object_id).await })
168+
.await
169+
.unwrap_or_else(|e| Err(AppError::from(e)));
165170

166171
match result {
167172
Ok(_) => Ok(Response::builder(StatusCode::NoContent).build()),

0 commit comments

Comments
 (0)