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
use crate::server;
use crate::{Request, Response};

use async_trait::async_trait;
use server::endpoint::DynEndpoint;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

/// 异步中间件trait
#[async_trait]
pub trait Middleware<State>: Send + Sync + 'static {
    /// 异步处理请求并返回响应。
    async fn handle(&self, request: Request<State>, next: Next<'_, State>) -> crate::Result;

    /// 设置中间件的名称。默认情况下,使用类型名字.
    fn name(&self) -> &str {
        std::any::type_name::<Self>()
    }
}

#[async_trait]
impl<State, F> Middleware<State> for F
where
    State: Clone + Send + Sync + 'static,
    F: Send
        + Sync
        + 'static
        + for<'a> Fn(
            Request<State>,
            Next<'a, State>,
        ) -> Pin<Box<dyn Future<Output = crate::Result> + 'a + Send>>,
{
    async fn handle(&self, req: Request<State>, next: Next<'_, State>) -> crate::Result {
        (self)(req, next).await
    }
}

/// 中间件链系列其余部分,包括endpoints。
#[allow(missing_debug_implementations)]
pub struct Next<'a, State> {
    pub(crate) endpoint: &'a DynEndpoint<State>,
    pub(crate) next_middleware: &'a [Arc<dyn Middleware<State>>],
}

impl<State: Clone + Send + Sync + 'static> Next<'_, State> {
    /// 异步执行其余的中间件。
    pub async fn run(mut self, req: Request<State>) -> Response {
        if let Some((current, next)) = self.next_middleware.split_first() {
            self.next_middleware = next;
            match current.handle(req, self).await {
                Ok(request) => request,
                Err(err) => err.into(),
            }
        } else {
            match self.endpoint.call(req).await {
                Ok(request) => request,
                Err(err) => err.into(),
            }
        }
    }
}