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
use error::Catch;
use routing::{Resource, RoutedService};
use service::WebService;
use util::Never;
use util::http::{HttpMiddleware};

use futures::future::{self, FutureResult};
use http;
use tower_service::NewService;

use std::fmt;

/// Creates new `WebService` values.
///
/// Instances of this type are created by `ServiceBuilder`. A `NewWebService`
/// instance is used to generate a `WebService` instance per connection.
pub struct NewWebService<T, U, M>
where
    T: Resource,
{
    /// The routed service. This service implements `Clone`.
    service: RoutedService<T, U>,

    /// Middleware to wrap the routed service with
    middleware: M,
}

impl<T, U, M> NewWebService<T, U, M>
where
    T: Resource,
    U: Catch,
    M: HttpMiddleware<RoutedService<T, U>>,
{
    /// Create a new `NewWebService` instance.
    pub(crate) fn new(service: RoutedService<T, U>, middleware: M) -> Self {
        NewWebService {
            service,
            middleware,
        }
    }
}

impl<T, U, M> NewService for NewWebService<T, U, M>
where
    T: Resource,
    U: Catch,
    M: HttpMiddleware<RoutedService<T, U>>,
{
    type Request = http::Request<M::RequestBody>;
    type Response = http::Response<M::ResponseBody>;
    type Error = M::Error;
    type Service = WebService<T, U, M>;
    type InitError = Never;
    type Future = FutureResult<Self::Service, Self::InitError>;

    fn new_service(&self) -> Self::Future {
        let service = self.middleware.wrap_http(self.service.clone());

        future::ok(WebService::new(service))
    }
}

impl<T, U, M> fmt::Debug for NewWebService<T, U, M>
where
    T: Resource + fmt::Debug,
    T::Destination: fmt::Debug,
    U: fmt::Debug,
    M: fmt::Debug,
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct("NewService")
            .field("service", &self.service)
            .field("middleware", &self.middleware)
            .finish()
    }
}