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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use std::{marker::PhantomData, time::Duration};

use hyper::{
    body::Incoming,
    http::{Method, StatusCode},
};
use motore::{layer::Layer, service::Service};

use crate::{
    handler::HandlerWithoutRequest,
    request::Request,
    response::{IntoResponse, Response},
    HttpContext,
};

pub trait LayerExt {
    fn method(
        self,
        method: Method,
    ) -> FilterLayer<Box<dyn Fn(&mut HttpContext, &Request) -> Result<(), StatusCode>>>
    where
        Self: Sized,
    {
        self.filter(Box::new(move |cx: &mut HttpContext, _: &Request| {
            if cx.method == method {
                Ok(())
            } else {
                Err(StatusCode::METHOD_NOT_ALLOWED)
            }
        }))
    }

    fn filter<F>(self, f: F) -> FilterLayer<F>
    where
        Self: Sized,
        F: Fn(&mut HttpContext, &Request) -> Result<(), StatusCode>,
    {
        FilterLayer { f }
    }
}

pub struct FilterLayer<F> {
    f: F,
}

impl<S, F> Layer<S> for FilterLayer<F>
where
    S: Service<HttpContext, Request, Response = Response> + Send + Sync + 'static,
    F: Fn(&mut HttpContext, &Request) -> Result<(), StatusCode> + Send + Sync,
{
    type Service = Filter<S, F>;

    fn layer(self, inner: S) -> Self::Service {
        Filter {
            service: inner,
            f: self.f,
        }
    }
}

pub struct Filter<S, F> {
    service: S,
    f: F,
}

impl<S, F> Service<HttpContext, Request> for Filter<S, F>
where
    S: Service<HttpContext, Request, Response = Response> + Send + Sync + 'static,
    F: Fn(&mut HttpContext, &Request) -> Result<(), StatusCode> + Send + Sync,
{
    type Response = S::Response;

    type Error = S::Error;

    async fn call<'s, 'cx>(
        &'s self,
        cx: &'cx mut HttpContext,
        req: Request,
    ) -> Result<Self::Response, Self::Error> {
        if let Err(status) = (self.f)(cx, &req) {
            return Ok(status.into_response());
        }
        self.service.call(cx, req).await
    }
}

#[derive(Clone)]
pub struct TimeoutLayer<H, T> {
    duration: Duration,
    handler: H,
    _marker: PhantomData<T>,
}

impl<H, T> TimeoutLayer<H, T> {
    pub fn new(duration: Duration, handler: H) -> Self
    where
        H: HandlerWithoutRequest<T> + Clone + Send + Sync + 'static,
    {
        Self {
            duration,
            handler,
            _marker: PhantomData,
        }
    }
}

impl<S, H, T> Layer<S> for TimeoutLayer<H, T>
where
    S: Service<HttpContext, Incoming, Response = Response> + Send + Sync + 'static,
    H: HandlerWithoutRequest<T> + Clone + Send + Sync + 'static,
    T: Sync,
{
    type Service = Timeout<S, H, T>;

    fn layer(self, inner: S) -> Self::Service {
        Timeout {
            service: inner,
            duration: self.duration,
            handler: self.handler,
            _marker: PhantomData,
        }
    }
}

#[derive(Clone)]
pub struct Timeout<S, H, T> {
    service: S,
    duration: Duration,
    handler: H,
    _marker: PhantomData<T>,
}

impl<S, H, T> Service<HttpContext, Incoming> for Timeout<S, H, T>
where
    S: Service<HttpContext, Incoming, Response = Response> + Send + Sync + 'static,
    S::Error: Send,
    H: HandlerWithoutRequest<T> + Clone + Send + Sync + 'static,
    T: Sync,
{
    type Response = S::Response;

    type Error = S::Error;

    async fn call<'s, 'cx>(
        &'s self,
        cx: &'cx mut HttpContext,
        req: Incoming,
    ) -> Result<Self::Response, Self::Error> {
        let fut_service = self.service.call(cx, req);
        let fut_timeout = tokio::time::sleep(self.duration);

        tokio::select! {
            resp = fut_service => resp,
            _ = fut_timeout => {
                Ok(self.handler.clone().call(cx).await)
            },
        }
    }
}