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
pub mod transport;
pub use self::transport::Transport;

use std::io;

use futures::{future, Future, Poll};
use http::{Request, Response};
use hyper;
use hyper::server::conn::Http;
use tower_service::{NewService, Service};

use rt;
use service::http::imp::{HttpRequestImpl, HttpResponseImpl};
use service::http::{HttpRequest, HttpResponse, RequestBody};
use CritError;

#[allow(missing_debug_implementations)]
pub struct Server<S, Tr = ()> {
    new_service: S,
    transport: Tr,
    protocol: Http,
}

impl<S> Server<S> {
    pub fn new(new_service: S) -> Server<S> {
        Server {
            new_service,
            transport: (),
            protocol: Http::new(),
        }
    }
}

impl<S, T> Server<S, T> {
    pub fn transport<Tr>(self, transport: Tr) -> Server<S, Tr>
    where
        Tr: Transport,
    {
        Server {
            new_service: self.new_service,
            transport,
            protocol: self.protocol,
        }
    }

    pub fn protocol(self, protocol: Http) -> Server<S, T> {
        Server { protocol, ..self }
    }
}

impl<S, Tr> Server<S, Tr>
where
    S: NewService,
    S::Request: HttpRequest,
    S::Response: HttpResponse,
    S::Error: Into<CritError>,
    S::InitError: Into<CritError>,
    Tr: Transport,
{
    pub fn run_forever(self) -> io::Result<()>
    where
        S: Send + 'static,
        <S::Service as Service>::Future: Send + 'static,
        S::Service: Send + 'static,
        S::Future: Send + 'static,
    {
        self.run_until(future::empty::<(), ()>())
    }

    pub fn run_until<F>(self, signal: F) -> io::Result<()>
    where
        S: Send + 'static,
        <S::Service as Service>::Future: Send + 'static,
        S::Service: Send + 'static,
        S::Future: Send + 'static,
        F: Future<Item = ()> + Send + 'static,
    {
        let incoming = self.transport.incoming()?;
        let builder = hyper::server::Builder::new(incoming, self.protocol);
        let new_service = LiftedNewHttpService(self.new_service);
        let serve = builder
            .serve(new_service)
            .with_graceful_shutdown(signal)
            .map_err(|e| error!("{}", e));
        rt::run(serve);
        Ok(())
    }
}

#[allow(missing_debug_implementations)]
struct LiftedNewHttpService<S>(S);

impl<S> hyper::service::NewService for LiftedNewHttpService<S>
where
    S: NewService,
    S::Request: HttpRequest,
    S::Response: HttpResponse,
    S::Error: Into<CritError>,
    S::InitError: Into<CritError>,
{
    type ReqBody = hyper::Body;
    type ResBody = <S::Response as HttpResponseImpl>::Body;
    type Error = S::Error;
    type Service = LiftedHttpService<S::Service>;
    type InitError = S::InitError;
    type Future = future::Map<S::Future, fn(S::Service) -> LiftedHttpService<S::Service>>;

    fn new_service(&self) -> Self::Future {
        self.0.new_service().map(LiftedHttpService)
    }
}

#[allow(missing_debug_implementations)]
struct LiftedHttpService<S>(S);

impl<S> hyper::service::Service for LiftedHttpService<S>
where
    S: Service,
    S::Request: HttpRequest,
    S::Response: HttpResponse,
    S::Error: Into<CritError>,
{
    type ReqBody = hyper::Body;
    type ResBody = <S::Response as HttpResponseImpl>::Body;
    type Error = S::Error;
    type Future = LiftedHttpServiceFuture<S::Future>;

    #[inline]
    fn call(&mut self, request: Request<hyper::Body>) -> Self::Future {
        let request = S::Request::from_request(request.map(|body| RequestBody(body)));
        LiftedHttpServiceFuture(self.0.call(request))
    }
}

#[allow(missing_debug_implementations)]
struct LiftedHttpServiceFuture<F>(F);

impl<F> Future for LiftedHttpServiceFuture<F>
where
    F: Future,
    F::Item: HttpResponse,
    F::Error: Into<CritError>,
{
    type Item = Response<<F::Item as HttpResponseImpl>::Body>;
    type Error = F::Error;

    #[inline]
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.0
            .poll()
            .map(|x| x.map(|response| response.into_response()))
    }
}