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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! The implementation of HTTP server based on hyper and tower-service.

pub mod middleware;

pub use self::imp::{start, ServerConfig, ServerError, ServerResult, ServiceBuilder};

mod imp {
    use std::error;
    use std::fmt;
    use std::net::{IpAddr, SocketAddr};
    use std::sync::Arc;

    use failure;
    use failure::Fallible;
    use futures::{future, Future, Stream};
    use http::{Request, Response};
    use hyper::body::{Body, Payload};
    use hyper::server as hyper_server;
    use hyper::server::conn::AddrIncoming;
    use hyper::service as hyper_service;
    use tokio::io::{AsyncRead, AsyncWrite};
    use tokio::runtime::Runtime;
    use tower_service;
    use tower_service::NewService;
    #[cfg(feature = "tower-web")]
    use tower_web;

    use app::App;
    use endpoint::OutputEndpoint;
    use rt::{with_set_runtime_mode, RuntimeMode};

    #[cfg(feature = "tower-web")]
    use super::middleware::TowerWebMiddleware;
    use super::middleware::{Chain, Middleware};

    /// The error type which will be returned from `ServiceBuilder::serve()`.
    #[derive(Debug)]
    pub struct ServerError {
        inner: failure::Error,
    }

    impl fmt::Display for ServerError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "failed to start the server: {}", self.inner)
        }
    }

    impl error::Error for ServerError {
        fn description(&self) -> &str {
            "failed to start the server"
        }
    }

    /// A type alias of `Result<T, E>` whose error type is restrected to `ServerError`.
    pub type ServerResult<T> = Result<T, ServerError>;

    /// Create an instance of `ServiceBuilder` from the specified endpoint.
    pub fn start<E>(endpoint: E) -> ServiceBuilder<App<E>>
    where
        for<'a> E: OutputEndpoint<'a> + 'static,
    {
        ServiceBuilder::new(App::new(endpoint))
    }

    /// A builder of HTTP service.
    #[derive(Debug)]
    pub struct ServiceBuilder<S> {
        new_service: S,
    }

    impl<S> ServiceBuilder<S>
    where
        S: NewService,
    {
        /// Creates a new `ServerBuilder` from the specified NewService.
        pub fn new(new_service: S) -> Self {
            ServiceBuilder { new_service }
        }

        /// Wraps the inner service into the specified middleware.
        pub fn with_middleware<M>(self, middleware: M) -> ServiceBuilder<Chain<S, M>>
        where
            M: Middleware<S::Service> + Clone,
        {
            ServiceBuilder {
                new_service: Chain::new(self.new_service, middleware),
            }
        }

        /// Wraps the inner service into the specified Tower-web middleware.
        #[cfg(feature = "tower-web")]
        pub fn with_tower_middleware<M>(
            self,
            middleware: M,
        ) -> ServiceBuilder<Chain<S, TowerWebMiddleware<M>>>
        where
            M: tower_web::middleware::Middleware<S::Service>,
        {
            ServiceBuilder {
                new_service: Chain::new(self.new_service, TowerWebMiddleware::new(middleware)),
            }
        }
    }

    impl<S> NewService for ServiceBuilder<S>
    where
        S: NewService,
    {
        type Request = S::Request;
        type Response = S::Response;
        type Error = S::Error;
        type Service = S::Service;
        type InitError = S::InitError;
        type Future = S::Future;

        #[inline]
        fn new_service(&self) -> Self::Future {
            self.new_service.new_service()
        }
    }

    impl<S, Bd> ServiceBuilder<S>
    where
        S: NewService<Request = Request<Body>, Response = Response<Bd>> + Send + Sync + 'static,
        S::Error: Into<Box<dyn error::Error + Send + Sync + 'static>>,
        S::InitError: Into<Box<dyn error::Error + Send + Sync + 'static>>,
        S::Service: Send,
        S::Future: Send + 'static,
        <S::Service as tower_service::Service>::Future: Send + 'static,
        Bd: Payload,
    {
        /// Start the server with the specified configuration.
        pub fn serve(self, config: impl ServerConfig) -> ServerResult<()> {
            self.serve_with_graceful_shutdown(config, future::empty::<(), ()>())
        }

        /// Start the server with the specified configuration and shutdown signal.
        pub fn serve_with_graceful_shutdown(
            self,
            server_config: impl ServerConfig,
            signal: impl Future<Item = ()> + Send + 'static,
        ) -> ServerResult<()> {
            server_config
                .build()
                .map_err(|inner| ServerError { inner })?
                .serve_with_graceful_shutdown(self, signal)
                .map_err(|inner| ServerError { inner })
        }
    }

    #[derive(Debug)]
    pub struct Server<I> {
        builder: hyper_server::Builder<I>,
        rt: Runtime,
    }

    impl<I> Server<I>
    where
        I: Stream + Send + 'static,
        I::Item: AsyncRead + AsyncWrite + Send + 'static,
        I::Error: Into<Box<dyn error::Error + Send + Sync + 'static>>,
    {
        fn serve_with_graceful_shutdown<S, Bd>(
            self,
            new_service: S,
            signal: impl Future<Item = ()> + Send + 'static,
        ) -> Fallible<()>
        where
            S: NewService<Request = Request<Body>, Response = Response<Bd>> + Send + Sync + 'static,
            S::Error: Into<Box<dyn error::Error + Send + Sync + 'static>>,
            S::InitError: Into<Box<dyn error::Error + Send + Sync + 'static>>,
            S::Service: Send,
            S::Future: Send + 'static,
            <S::Service as tower_service::Service>::Future: Send + 'static,
            Bd: Payload,
        {
            let Self { builder, mut rt } = self;

            // Put the instance of new_service into the heap and ensure that
            // it lives until enter the scope.
            //
            // It implies that all tasks spawned by Tokio runtime must be dropped
            // after executing `shutdown_on_idle()`.
            let new_service = Arc::new(new_service);

            let mut server = builder
                .serve(NewHttpService(new_service.clone()))
                .with_graceful_shutdown(signal)
                .map_err(|err| error!("server error: {}", err));

            let server = future::poll_fn(move || {
                with_set_runtime_mode(RuntimeMode::ThreadPool, || server.poll())
            });

            rt.spawn(server);
            rt.shutdown_on_idle().wait().unwrap();
            Ok(())
        }
    }

    /// A trait representing the configuration to start the HTTP server.
    pub trait ServerConfig: ServerConfigImpl {}

    impl<'a> ServerConfig for &'a str {}
    impl ServerConfig for String {}
    impl<I: Into<IpAddr>> ServerConfig for (I, u16) {}
    impl ServerConfig for SocketAddr {}
    impl<'a> ServerConfig for &'a SocketAddr {}

    pub trait ServerConfigImpl {
        type Item: AsyncRead + AsyncWrite + Send + 'static;
        type Error: Into<Box<dyn error::Error + Send + Sync + 'static>>;
        type Incoming: Stream<Item = Self::Item, Error = Self::Error> + Send + 'static;

        fn build(self) -> Fallible<Server<Self::Incoming>>;
    }

    impl<'a> ServerConfigImpl for &'a str {
        type Item = <AddrIncoming as Stream>::Item;
        type Error = <AddrIncoming as Stream>::Error;
        type Incoming = AddrIncoming;

        fn build(self) -> Fallible<Server<Self::Incoming>> {
            ServerConfigImpl::build(&self.parse::<SocketAddr>()?)
        }
    }

    impl ServerConfigImpl for String {
        type Item = <AddrIncoming as Stream>::Item;
        type Error = <AddrIncoming as Stream>::Error;
        type Incoming = AddrIncoming;

        fn build(self) -> Fallible<Server<Self::Incoming>> {
            ServerConfigImpl::build(self.as_str())
        }
    }

    impl<I: Into<IpAddr>> ServerConfigImpl for (I, u16) {
        type Item = <AddrIncoming as Stream>::Item;
        type Error = <AddrIncoming as Stream>::Error;
        type Incoming = AddrIncoming;

        fn build(self) -> Fallible<Server<Self::Incoming>> {
            ServerConfigImpl::build(SocketAddr::from(self))
        }
    }

    impl ServerConfigImpl for SocketAddr {
        type Item = <AddrIncoming as Stream>::Item;
        type Error = <AddrIncoming as Stream>::Error;
        type Incoming = AddrIncoming;

        fn build(self) -> Fallible<Server<Self::Incoming>> {
            ServerConfigImpl::build(&self)
        }
    }

    impl<'a> ServerConfigImpl for &'a SocketAddr {
        type Item = <AddrIncoming as Stream>::Item;
        type Error = <AddrIncoming as Stream>::Error;
        type Incoming = AddrIncoming;

        fn build(self) -> Fallible<Server<Self::Incoming>> {
            let builder = hyper_server::Server::try_bind(self)?;
            let rt = Runtime::new()?;
            Ok(Server { builder, rt })
        }
    }

    #[derive(Debug)]
    struct NewHttpService<S>(S);

    impl<S, ReqBody, ResBody> hyper_service::NewService for NewHttpService<S>
    where
        S: tower_service::NewService<Request = Request<ReqBody>, Response = Response<ResBody>>,
        ReqBody: Payload,
        ResBody: Payload,
        S::Error: Into<Box<dyn error::Error + Send + Sync + 'static>>,
        S::InitError: Into<Box<dyn error::Error + Send + Sync + 'static>>,
    {
        type ReqBody = ReqBody;
        type ResBody = ResBody;
        type Error = S::Error;
        type Service = HttpService<S::Service>;
        type InitError = S::InitError;
        #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
        type Future = future::Map<S::Future, fn(S::Service) -> HttpService<S::Service>>;

        #[inline]
        fn new_service(&self) -> Self::Future {
            self.0.new_service().map(HttpService)
        }
    }

    #[derive(Debug)]
    struct HttpService<S>(S);

    impl<S, ReqBody, ResBody> hyper_service::Service for HttpService<S>
    where
        S: tower_service::Service<Request = Request<ReqBody>, Response = Response<ResBody>>,
        ReqBody: Payload,
        ResBody: Payload,
        S::Error: Into<Box<dyn error::Error + Send + Sync + 'static>>,
    {
        type ReqBody = ReqBody;
        type ResBody = ResBody;
        type Error = S::Error;
        type Future = S::Future;

        #[inline]
        fn call(&mut self, request: Request<ReqBody>) -> Self::Future {
            self.0.call(request)
        }
    }
}