-
Notifications
You must be signed in to change notification settings - Fork 157
/
json.rs
75 lines (63 loc) · 2.22 KB
/
json.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#[macro_use]
extern crate nickel;
use serde_json;
#[macro_use]
extern crate serde_derive;
use async_trait::async_trait;
use nickel::status::StatusCode;
use nickel::{HttpRouter, MediaType, Nickel, Middleware, MiddlewareResult, Request, Response};
#[derive(Serialize, Deserialize)]
struct Person {
first_name: String,
last_name: String,
}
struct JsonPost;
#[async_trait]
impl Middleware<()> for JsonPost {
async fn invoke(&self, req: &mut Request, res: Response) -> MiddlewareResult {
let person = try_with!(res, {
req.json_as::<Person>().await
});
res.send(format!("Hello {} {}", person.first_name, person.last_name))
}
}
#[tokio::main]
async fn main() {
let mut server = Nickel::new();
// try it with curl
// curl 'http://localhost:6767' -H 'Content-Type: application/json;charset=UTF-8' --data-binary $'{ "first_name": "John","last_name": "Connor" }'
server.post("/", JsonPost);
// TODO: the middleware macro has not yet been updated to support async
// server.post(
// "/",
// middleware! { |request, response|
// let person = try_with!(response, {
// request.json_as::<Person>().await.map_err(|e| (StatusCode::BAD_REQUEST, e))
// });
// format!("Hello {} {}", person.first_name, person.last_name)
// },
// );
// go to http://localhost:6767/your/name to see this route in action
server.get(
"/:first/:last",
middleware! { |req|
// These unwraps are safe because they are required parts of the route
let first_name = req.param("first").unwrap();
let last_name = req.param("last").unwrap();
let person = Person {
first_name: first_name.to_string(),
last_name: last_name.to_string(),
};
serde_json::to_value(person).map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e))
},
);
// go to http://localhost:6767/raw to see this route in action
server.get(
"/raw",
middleware! { |_, mut response|
response.set(MediaType::Json);
r#"{ "foo": "bar" }"#
},
);
server.listen("127.0.0.1:6767").await.unwrap();
}