-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.rs
31 lines (28 loc) · 1.21 KB
/
middleware.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
use amiya::m;
fn main() {
// The middleware system of Amiya uses onion model, just as NodeJs's koa framework.
// The executed order is:
// - `Logger`'s code before `next()`, which print a log about request in
// - `Respond`'s code before `next()`, which do nothing
// - `Respond`'s code after `next()`, which set the response body
// - `Logger`'s code after `next()`, which read the response body and log it
let app = amiya::new()
// Let's call This middleware `Logger`
// `ctx.next().await` will return after all inner middleware be executed
// so the `content` will be "Hello World" , which is set by next middleware.
.uses(m!(ctx =>
println!("new request at");
ctx.next().await?;
let content = ctx.resp.take_body().into_string().await.unwrap();
println!("finish, response is: {}", content);
ctx.resp.set_body(content);
))
// Let's call This middleware `Respond`
// This middleware set tht response
.uses(m!(ctx =>
ctx.next().await?;
ctx.resp.set_body("Hello World!");
));
app.listen("[::]:8080").unwrap();
std::thread::park();
}