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
use super::handler::{BoxHandler, Handler};
use super::{BoxError, BoxFuture, Request, Response};
use crate::router::OwnedCaptures;

use crate::http_router::{HttpRouter, Method};

use std::sync::Arc;
use std::task::{Context, Poll};

use hyper::service::Service;

#[derive(Debug)]
pub struct RouterService<H = BoxHandler> {
    router: HttpRouter<H>,
    default: H,
}

#[derive(Debug)]
pub struct SharedRouterService<H = BoxHandler>(Arc<RouterService<H>>);

impl<H> Clone for SharedRouterService<H> {
    fn clone(&self) -> Self {
        Self(Arc::clone(&self.0))
    }
}

impl<H> Service<Request> for RouterService<H>
where
    H: Handler + Send + Sync,
{
    type Response = Response;
    type Error = BoxError;
    type Future = BoxFuture<'static, Result<Response, BoxError>>;

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

    fn call(&mut self, req: Request) -> Self::Future {
        RouterService::handle(self, req)
    }
}

impl<H> Service<Request> for SharedRouterService<H>
where
    H: Handler + Send + Sync,
{
    type Response = Response;
    type Error = BoxError;
    type Future = BoxFuture<'static, Result<Response, BoxError>>;

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

    fn call(&mut self, req: Request) -> Self::Future {
        RouterService::handle(&*self.0, req)
    }
}

impl<H> RouterService<H>
where
    H: Handler,
{
    fn handle(&self, req: Request) -> BoxFuture<'static, Result<Response, BoxError>> {
        let method = req.method();
        let path = req.uri().path();
        let (handler, params) = match self.router.find(method, path) {
            Some((h, caps)) => (h, OwnedCaptures::new(&caps)),
            None => (&self.default, OwnedCaptures::empty()),
        };
        Handler::call(handler, req, params)
    }

    pub fn new(default: H) -> Self {
        Self::from_router(HttpRouter::new(), default)
    }

    pub fn from_router(router: HttpRouter<H>, default: H) -> Self {
        Self { router, default }
    }

    pub fn into_shared(self) -> SharedRouterService<H> {
        SharedRouterService(Arc::new(self))
    }
}

impl HttpRouter<BoxHandler> {
    pub fn route(
        &mut self,
        method: Method,
        path: &str,
        h: impl Handler + Send + Sync + 'static,
    ) -> &mut Self {
        self.insert(method, path, Box::new(h))
    }

    pub fn with_default(self, default: impl Handler + Send + Sync + 'static) -> RouterService {
        RouterService::from_router(self, Box::new(default))
    }
}

macro_rules! define_method{
    ($name:tt,$method:tt) => {
        pub fn $name(&mut self,path: &str,h: impl Handler+Send+Sync+'static) -> &mut Self{
            self.route(Method::$method,path,h)
        }
    }
}

impl HttpRouter<BoxHandler> {
    define_method!(get, GET);
    define_method!(post, POST);
    define_method!(put, PUT);
    define_method!(delete, DELETE);
    define_method!(head, HEAD);
    define_method!(options, OPTIONS);
    define_method!(connect, CONNECT);
    define_method!(patch, PATCH);
    define_method!(trace, TRACE);
}