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
use std::{future::Future, marker::PhantomData, pin::Pin};

use covert_types::{error::ApiError, request::Request, response::Response};
use tower::{Layer, Service, ServiceExt};

use super::{
    method_router::{MethodRouter, Route},
    SyncService,
};

pub struct Building;
pub struct Ready;

/// Wrapper around `matchit::Router`
pub struct Router<Stage = Building> {
    routes: Vec<(&'static str, MethodRouter)>,
    router: matchit::Router<MethodRouter>,
    _marker: PhantomData<Stage>,
}

impl Default for Router {
    fn default() -> Self {
        Self::new()
    }
}

impl Router {
    #[must_use]
    pub fn new() -> Self {
        Self {
            routes: Vec::default(),
            router: matchit::Router::default(),
            _marker: PhantomData,
        }
    }

    #[must_use]
    pub fn route(mut self, path: &'static str, route: MethodRouter) -> Self {
        self.routes.push((path, route));
        self
    }

    #[must_use]
    pub fn layer<L>(mut self, layer: L) -> Self
    where
        L: Layer<Route>,
        L::Service:
            Service<Request, Error = ApiError, Response = Response> + Clone + Send + 'static,
        <L::Service as Service<Request>>::Future: Send + 'static,
    {
        self.routes = self
            .routes
            .into_iter()
            .map(|(path, route)| (path, route.layer(&layer)))
            .collect();
        self
    }

    pub fn build(mut self) -> Router<Ready> {
        for (path, route) in self.routes.clone() {
            self.router
                .insert(path, route)
                .expect("No path should overlap");
        }
        Router::<Ready> {
            routes: self.routes,
            router: self.router,
            _marker: PhantomData,
        }
    }
}

impl Router<Ready> {
    // TODO: rename to `into_make_service`?
    pub fn into_service(self) -> SyncService<Request, Response> {
        SyncService::new(self)
    }
}

impl Clone for Router<Ready> {
    fn clone(&self) -> Self {
        Self {
            routes: self.routes.clone(),
            router: self.router.clone(),
            _marker: PhantomData,
        }
    }
}

impl Service<Request> for Router<Ready> {
    type Response = Response;

    type Error = ApiError;

    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(
        &mut self,
        _cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        std::task::Poll::Ready(Ok(()))
    }

    fn call(&mut self, mut req: Request) -> Self::Future {
        let prefixed_path = if req.path.starts_with('/') {
            req.path.clone()
        } else {
            format!("/{}", req.path)
        };
        let matched_router = match self.router.at(&prefixed_path) {
            Ok(r) => r,
            Err(_) => return Box::pin(async { Err(ApiError::not_found()) }),
        };
        req.params = matched_router
            .params
            .iter()
            .map(|(_key, val)| val.to_string())
            .collect();
        let matched_router = matched_router.value.clone();
        Box::pin(async move { matched_router.oneshot(req).await })
    }
}