-
Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathjson.rs
39 lines (32 loc) · 916 Bytes
/
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
use serde::{Deserialize, Serialize};
use tide::prelude::*; // Pulls in the json! macro.
use tide::{Body, Request};
#[derive(Deserialize, Serialize)]
struct Cat {
name: String,
}
#[async_std::main]
async fn main() -> tide::Result<()> {
femme::start();
let mut app = tide::new();
app.with(tide::log::LogMiddleware::new());
app.at("/submit").post(|mut req: Request<()>| async move {
let cat: Cat = req.body_json().await?;
println!("cat name: {}", cat.name);
let cat = Cat {
name: "chashu".into(),
};
Body::from_json(&cat)
});
app.at("/animals").get(|_| async {
Ok(json!({
"meta": { "count": 2 },
"animals": [
{ "type": "cat", "name": "chashu" },
{ "type": "cat", "name": "nori" }
]
}))
});
app.listen("127.0.0.1:8080").await?;
Ok(())
}