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
use crate::{Middleware, Next, Request, Response};
pub use async_trait::async_trait;
use std::future::Future;
#[derive(Debug)]
pub struct Before<F>(pub F);
#[async_trait]
impl<State, F, Fut> Middleware<State> for Before<F>
where
State: Clone + Send + Sync + 'static,
F: Fn(Request<State>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Request<State>> + Send + Sync + 'static,
{
async fn handle(&self, request: Request<State>, next: Next<'_, State>) -> crate::Result {
let request = (self.0)(request).await;
Ok(next.run(request).await)
}
}
#[derive(Debug)]
pub struct After<F>(pub F);
#[async_trait]
impl<State, F, Fut> Middleware<State> for After<F>
where
State: Clone + Send + Sync + 'static,
F: Fn(Response) -> Fut + Send + Sync + 'static,
Fut: Future<Output = crate::Result> + Send + Sync + 'static,
{
async fn handle(&self, request: Request<State>, next: Next<'_, State>) -> crate::Result {
let response = next.run(request).await;
(self.0)(response).await
}
}