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
use crate::Body;
use http::{Request, Response};
use std::convert::identity;
pub fn before(
f: impl Fn(Request<Body>) -> Request<Body> + Send + Sync + 'static,
) -> impl Middleware {
create(f, identity)
}
pub fn after(
f: impl Fn(Response<Body>) -> Response<Body> + Send + Sync + 'static,
) -> impl Middleware {
create(identity, f)
}
pub fn create(
request: impl Fn(Request<Body>) -> Request<Body> + Send + Sync + 'static,
response: impl Fn(Response<Body>) -> Response<Body> + Send + Sync + 'static,
) -> impl Middleware {
struct Impl<F, G>(F, G);
impl<F, G> Middleware for Impl<F, G>
where
F: Fn(Request<Body>) -> Request<Body> + Send + Sync + 'static,
G: Fn(Response<Body>) -> Response<Body> + Send + Sync + 'static,
{
fn filter_request(&self, request: Request<Body>) -> Request<Body> {
(self.0)(request)
}
fn filter_response(&self, response: Response<Body>) -> Response<Body> {
(self.1)(response)
}
}
Impl(request, response)
}
pub trait Middleware: Send + Sync + 'static {
fn filter_request(&self, request: Request<Body>) -> Request<Body> {
request
}
fn filter_response(&self, response: Response<Body>) -> Response<Body> {
response
}
}