tower_web/service/
new_service.rs

1use error::Catch;
2use routing::{Resource, RoutedService};
3use service::WebService;
4use util::Never;
5use util::http::{HttpMiddleware};
6
7use futures::future::{self, FutureResult};
8use http;
9use tower_service::NewService;
10
11use std::fmt;
12
13/// Creates new `WebService` values.
14///
15/// Instances of this type are created by `ServiceBuilder`. A `NewWebService`
16/// instance is used to generate a `WebService` instance per connection.
17pub struct NewWebService<T, U, M>
18where
19    T: Resource,
20{
21    /// The routed service. This service implements `Clone`.
22    service: RoutedService<T, U>,
23
24    /// Middleware to wrap the routed service with
25    middleware: M,
26}
27
28impl<T, U, M> NewWebService<T, U, M>
29where
30    T: Resource,
31    U: Catch,
32    M: HttpMiddleware<RoutedService<T, U>>,
33{
34    /// Create a new `NewWebService` instance.
35    pub(crate) fn new(service: RoutedService<T, U>, middleware: M) -> Self {
36        NewWebService {
37            service,
38            middleware,
39        }
40    }
41}
42
43impl<T, U, M> NewService for NewWebService<T, U, M>
44where
45    T: Resource,
46    U: Catch,
47    M: HttpMiddleware<RoutedService<T, U>>,
48{
49    type Request = http::Request<M::RequestBody>;
50    type Response = http::Response<M::ResponseBody>;
51    type Error = M::Error;
52    type Service = WebService<T, U, M>;
53    type InitError = Never;
54    type Future = FutureResult<Self::Service, Self::InitError>;
55
56    fn new_service(&self) -> Self::Future {
57        let service = self.middleware.wrap_http(self.service.clone());
58
59        future::ok(WebService::new(service))
60    }
61}
62
63impl<T, U, M> fmt::Debug for NewWebService<T, U, M>
64where
65    T: Resource + fmt::Debug,
66    T::Destination: fmt::Debug,
67    U: fmt::Debug,
68    M: fmt::Debug,
69{
70    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
71        fmt.debug_struct("NewService")
72            .field("service", &self.service)
73            .field("middleware", &self.middleware)
74            .finish()
75    }
76}