@@ -3,7 +3,7 @@ use mongodb::Database;
3
3
use bson:: oid:: ObjectId ;
4
4
use serde_json:: Value ;
5
5
use std:: sync:: Arc ;
6
- use tokio:: runtime:: Runtime ;
6
+ use tokio:: runtime:: Handle ;
7
7
8
8
use crate :: {
9
9
db:: mongodb:: MongoRepo ,
@@ -14,12 +14,12 @@ use crate::{
14
14
#[ derive( Clone ) ]
15
15
struct State {
16
16
repo : Arc < MongoRepo > ,
17
- runtime : Arc < Runtime > ,
17
+ runtime : Handle ,
18
18
}
19
19
20
20
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 ( ) ;
23
23
24
24
let state = State {
25
25
repo : Arc :: new ( MongoRepo :: new ( & db) ) ,
@@ -49,9 +49,10 @@ async fn create_restaurant(mut req: Request<State>) -> tide::Result {
49
49
let repo = req. state ( ) . repo . clone ( ) ;
50
50
let runtime = req. state ( ) . runtime . clone ( ) ;
51
51
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) ) ) ;
55
56
56
57
match result {
57
58
Ok ( created) => Ok ( Response :: builder ( StatusCode :: Created )
@@ -67,9 +68,10 @@ async fn list_restaurants(req: Request<State>) -> tide::Result {
67
68
let repo = req. state ( ) . repo . clone ( ) ;
68
69
let runtime = req. state ( ) . runtime . clone ( ) ;
69
70
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) ) ) ;
73
75
74
76
match result {
75
77
Ok ( restaurants) => Ok ( Response :: builder ( StatusCode :: Ok )
@@ -93,9 +95,10 @@ async fn get_restaurant(req: Request<State>) -> tide::Result {
93
95
let repo = req. state ( ) . repo . clone ( ) ;
94
96
let runtime = req. state ( ) . runtime . clone ( ) ;
95
97
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) ) ) ;
99
102
100
103
match result {
101
104
Ok ( restaurant) => Ok ( Response :: builder ( StatusCode :: Ok )
@@ -130,9 +133,10 @@ async fn update_restaurant(mut req: Request<State>) -> tide::Result {
130
133
let repo = req. state ( ) . repo . clone ( ) ;
131
134
let runtime = req. state ( ) . runtime . clone ( ) ;
132
135
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) ) ) ;
136
140
137
141
match result {
138
142
Ok ( updated) => Ok ( Response :: builder ( StatusCode :: Ok )
@@ -159,9 +163,10 @@ async fn delete_restaurant(req: Request<State>) -> tide::Result {
159
163
let repo = req. state ( ) . repo . clone ( ) ;
160
164
let runtime = req. state ( ) . runtime . clone ( ) ;
161
165
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) ) ) ;
165
170
166
171
match result {
167
172
Ok ( _) => Ok ( Response :: builder ( StatusCode :: NoContent ) . build ( ) ) ,
0 commit comments