@@ -12,6 +12,7 @@ It is a hobby project and is intended to be a learning experience for me. I am n
12
12
- [x] Router Middleware support
13
13
- [x] Static file serving
14
14
- [x] Compression (gzip)
15
+ - [x] JSON parsing (Thanks to [ serde_json] ( https://github.com/serde-rs/json ) )
15
16
16
17
## Getting Started
17
18
@@ -26,7 +27,7 @@ It is a hobby project and is intended to be a learning experience for me. I am n
26
27
27
28
``` toml
28
29
[dependencies ]
29
- krustie = " 0.1.4 "
30
+ krustie = " 0.1.5 "
30
31
```
31
32
32
33
or use ` cargo add ` in your terminal:
@@ -43,7 +44,9 @@ use krustie::{
43
44
router :: { Router , methods :: Endpoints },
44
45
response :: { HttpResponse , StatusCode },
45
46
request :: HttpRequest ,
46
- middleware :: { MiddlewareHandler , Middleware , gzip :: Gzip } };
47
+ middleware :: { MiddlewareHandler , Middleware , gzip :: Gzip },
48
+ json :: { json, get_string_from_json },
49
+ };
47
50
use std :: collections :: HashMap ;
48
51
use std :: net :: Ipv4Addr ;
49
52
@@ -62,19 +65,31 @@ fn main() {
62
65
63
66
sub_router
64
67
. get (| _ , res | {
65
- res . status (StatusCode :: Ok );
68
+ let body = json! ({" message" : " Hello, World!" });
69
+ res . status (StatusCode :: Ok ). json_body (body );
66
70
})
67
- . post (| _ , res | {
68
- res . status (StatusCode :: try_from (418 ). unwrap ());
69
- });
71
+ . post (post_req );
70
72
71
73
router . use_router (" home" , sub_router );
72
74
73
75
server . use_handler (router );
74
76
server . use_handler (AddKrustieHeader );
75
77
server . use_handler (Gzip );
78
+ }
76
79
77
- server . listen ();
80
+ fn post_req (req : & HttpRequest , res : & mut HttpResponse ) {
81
+ match req . get_body_as_json () {
82
+ Ok (body ) => {
83
+ if get_string_from_json (body . get (" server" )). unwrap () == " Krustie" {
84
+ res . status (StatusCode :: Ok ). json_body (body );
85
+ } else {
86
+ res . status (StatusCode :: try_from (201 ). unwrap ()). json_body (json! ({" error" : " Invalid server" }));
87
+ }
88
+ }
89
+ Err (_ ) => {
90
+ res . status (StatusCode :: BadRequest ). json_body (json! ({" error" : " Invalid JSON" }));
91
+ }
92
+ }
78
93
}
79
94
```
80
95
@@ -106,7 +121,6 @@ As an inexperienced developer contributions will be welcomed. Please open an iss
106
121
107
122
### Basic API Server Features
108
123
109
- - [ ] JSON parsing
110
124
- [ ] XML parsing
111
125
- [ ] Query parameter parsing
112
126
- [ ] Request validation
0 commit comments