File tree Expand file tree Collapse file tree 4 files changed +22
-2
lines changed Expand file tree Collapse file tree 4 files changed +22
-2
lines changed Original file line number Diff line number Diff line change 1
- use async_std:: io;
2
1
use async_std:: task;
3
2
use serde:: { Deserialize , Serialize } ;
3
+ use tide:: prelude:: * ;
4
4
use tide:: { Request , Response , StatusCode } ;
5
5
6
6
#[ derive( Deserialize , Serialize ) ]
7
7
struct Cat {
8
8
name : String ,
9
9
}
10
10
11
- fn main ( ) -> io :: Result < ( ) > {
11
+ fn main ( ) -> tide :: Result < ( ) > {
12
12
task:: block_on ( async {
13
13
let mut app = tide:: new ( ) ;
14
14
@@ -23,6 +23,16 @@ fn main() -> io::Result<()> {
23
23
Ok ( Response :: new ( StatusCode :: Ok ) . body_json ( & cat) ?)
24
24
} ) ;
25
25
26
+ app. at ( "/animals" ) . get ( |_| async {
27
+ Ok ( json ! ( {
28
+ "meta" : { "count" : 2 } ,
29
+ "animals" : [
30
+ { "type" : "cat" , "name" : "chashu" } ,
31
+ { "type" : "cat" , "name" : "nori" }
32
+ ]
33
+ } ) )
34
+ } ) ;
35
+
26
36
app. listen ( "127.0.0.1:8080" ) . await ?;
27
37
Ok ( ( ) )
28
38
} )
Original file line number Diff line number Diff line change @@ -216,6 +216,7 @@ pub use redirect::Redirect;
216
216
pub use request:: Request ;
217
217
pub use response:: Response ;
218
218
pub use route:: Route ;
219
+ pub use serde_json;
219
220
pub use server:: Server ;
220
221
221
222
#[ doc( inline) ]
Original file line number Diff line number Diff line change 1
1
//! The Tide prelude.
2
2
pub use http_types:: Status ;
3
+ pub use serde_json:: json;
Original file line number Diff line number Diff line change @@ -335,6 +335,14 @@ impl Into<http::Response> for Response {
335
335
}
336
336
}
337
337
338
+ impl From < serde_json:: Value > for Response {
339
+ fn from ( json_value : serde_json:: Value ) -> Self {
340
+ Response :: new ( StatusCode :: Ok )
341
+ . body_json ( & json_value)
342
+ . unwrap_or_else ( |_| Response :: new ( StatusCode :: InternalServerError ) )
343
+ }
344
+ }
345
+
338
346
impl From < http:: Response > for Response {
339
347
fn from ( res : http:: Response ) -> Self {
340
348
Self {
You can’t perform that action at this time.
0 commit comments