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
#![doc(
    html_root_url = "https://docs.rs/spirit-hyper/0.1.0/spirit_hyper/",
    test(attr(deny(warnings)))
)]
#![forbid(unsafe_code)]
#![warn(missing_docs)]

//! [Spirit] helper for Hyper
//!
//! This allows having Hyper servers auto-spawned from configuration. It is possible to put them on
//! top of arbitrary stream-style IO objects (TcpStream, UdsStream, these wrapped in SSL...).
//!
//! # Examples
//!
//! ```rust
//! #![type_length_limit="8388608"]
//! extern crate failure;
//! extern crate hyper;
//! extern crate serde;
//! #[macro_use]
//! extern crate serde_derive;
//! extern crate spirit;
//! extern crate spirit_hyper;
//!
//! use std::default::Default;
//!
//! use hyper::{Body, Request, Response};
//! use spirit::{Empty, Spirit, SpiritInner};
//! use spirit_hyper::HttpServer;
//!
//! const DEFAULT_CONFIG: &str = r#"
//! [server]
//! port = 1234
//! "#;
//! #[derive(Default, Deserialize)]
//! struct Config {
//!     server: HttpServer,
//! }
//!
//! impl Config {
//!     fn server(&self) -> HttpServer {
//!         self.server.clone()
//!     }
//! }
//!
//! fn request(_: &SpiritInner<Empty, Config>, _req: Request<Body>, _: &Empty) -> Response<Body> {
//!     Response::new(Body::from("Hello world\n"))
//! }
//!
//! fn main() {
//!     Spirit::<_, Empty, _>::new(Config::default())
//!         .config_defaults(DEFAULT_CONFIG)
//!         .config_helper(Config::server, spirit_hyper::service_fn_ok(request), "Server")
//!         .run(|spirit| {
//! #           spirit.terminate();
//!             Ok(())
//!         });
//! }
//! ```
//! Further examples are in the
//! [git repository](https://github.com/vorner/spirit/tree/master/spirit-hyper/examples).
//!
//! # Known problems
//!
//! * Not many helper generators are present ‒ only [`service_fn_ok`](fn.service_fn_ok.html) for
//!   now  And that one doesn't (yet) support futures.
//! * It creates some huge types under the hood. For now, if the compiler complains about
//!   `type_length_limit`, try increasing it (maybe even multiple times). This might be overcome in
//!   the future, but for now, the main downside to it is probably compile times.
//!
//! [Spirit]: https://crates.io/crates/spirit.

extern crate arc_swap;
extern crate failure;
extern crate futures;
extern crate hyper;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate spirit;
extern crate spirit_tokio;
extern crate structopt;
extern crate tokio_io;

use std::borrow::Borrow;
use std::error::Error;
use std::fmt::{Debug, Display};
use std::iter;
use std::sync::Arc;

use arc_swap::ArcSwap;
use failure::Error as FailError;
use futures::future::Shared;
use futures::sync::oneshot::{self, Receiver};
use futures::{Async, Future, IntoFuture, Poll};
use hyper::body::Payload;
use hyper::server::conn::{Connection, Http};
use hyper::service::{self, Service};
use hyper::{Body, Request, Response};
use serde::Deserialize;
use spirit::helpers::{CfgHelper, IteratedCfgHelper};
use spirit::{Builder, Empty, Spirit};
use spirit_tokio::{ResourceMaker, TcpListen};
use structopt::StructOpt;
use tokio_io::{AsyncRead, AsyncWrite};

/// A configuration fragment that can spawn Hyper server.
///
/// This is `CfgHelper` and `IteratedCfgHelper` ‒ you can use either singular or a container as the
/// configuration fragment and pair it with a request handler.
///
/// The `Transport` type parameter is subfragment that describes the listening socket or something
/// similar ‒ for example `spirit_tokio::TcpListen`.
///
/// The request handler must implement the [`ConnAction`](trait.ConnAction.html) trait.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct HyperServer<Transport> {
    #[serde(flatten)]
    transport: Transport,
}

/// A type alias for http (plain TCP) hyper server.
pub type HttpServer<ExtraCfg = Empty> = HyperServer<TcpListen<ExtraCfg>>;

struct ShutdownConn<T, S: Service> {
    conn: Connection<T, S>,
    shutdown: Option<Shared<Receiver<()>>>,
}

impl<T, S, B> Future for ShutdownConn<T, S>
where
    S: Service<ReqBody = Body, ResBody = B> + 'static,
    S::Error: Into<Box<Error + Send + Sync>>,
    S::Future: Send,
    T: AsyncRead + AsyncWrite + 'static,
    B: Payload + 'static,
{
    type Item = <Connection<T, S> as Future>::Item;
    type Error = <Connection<T, S> as Future>::Error;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let do_shutdown = self
            .shutdown
            .as_mut()
            .map(Future::poll)
            .unwrap_or(Ok(Async::NotReady));
        match do_shutdown {
            Ok(Async::NotReady) => (), // Don't shutdown yet (or already done that)
            _ => {
                self.conn.graceful_shutdown();
                self.shutdown.take();
            }
        }
        self.conn.poll()
    }
}

/// A trait describing the connection action.
///
/// This is usually a function
/// `Fn(&Arc<Spirit>, &ExtraCfg) -> impl IntoFuture<Item = (impl Service, Http), Error>`
///
/// It is similar to `hyper::NewService`, but with extra access to the spirit and extra config from
/// the transport fragment.
pub trait ConnAction<S, O, C, ExtraCfg>
where
    S: Borrow<ArcSwap<C>> + Sync + Send + 'static,
{
    /// The type the function-like thing returns.
    type IntoFuture;

    /// Performs the action.
    ///
    /// This should create the `hyper::Service` used to handle a connection.
    fn action(&self, &Arc<Spirit<S, O, C>>, &ExtraCfg) -> Self::IntoFuture;
}

impl<F, S, O, C, ExtraCfg, R> ConnAction<S, O, C, ExtraCfg> for F
where
    F: Fn(&Arc<Spirit<S, O, C>>, &ExtraCfg) -> R,
    S: Borrow<ArcSwap<C>> + Sync + Send + 'static,
{
    type IntoFuture = R;
    fn action(&self, arc: &Arc<Spirit<S, O, C>>, extra: &ExtraCfg) -> R {
        self(arc, extra)
    }
}

/// A helper to create a [`ConnAction`](trait.ConnAction.html) from a function (or closure).
///
/// This turns a `Fn(&Arc<Spirit>, &Request<Body>, &ExtraCfg) -> Response<Body>` into a
/// `ConnAction`, so it can be fed into `cfg_helper`.
pub fn service_fn_ok<F, S, O, C, ExtraCfg>(
    f: F,
) -> impl ConnAction<
    S,
    O,
    C,
    ExtraCfg,
    IntoFuture = Result<
        (
            impl Service<ReqBody = Body, Future = impl Send> + Send,
            Http,
        ),
        FailError,
    >,
>
where
    // TODO: Make more generic ‒ return future, payload, ...
    F: Fn(&Arc<Spirit<S, O, C>>, Request<Body>, &ExtraCfg) -> Response<Body>
        + Send
        + Sync
        + 'static,
    ExtraCfg: Clone + Debug + PartialEq + Send + 'static,
    S: Borrow<ArcSwap<C>> + Sync + Send + 'static,
    for<'de> C: Deserialize<'de> + Send + Sync + 'static,
    O: Debug + StructOpt + Sync + Send + 'static,
{
    let f = Arc::new(f);
    move |spirit: &_, extra_cfg: &ExtraCfg| -> Result<_, FailError> {
        let spirit = Arc::clone(spirit);
        let extra_cfg = extra_cfg.clone();
        let f = Arc::clone(&f);
        let svc = move |req: Request<Body>| -> Response<Body> { f(&spirit, req, &extra_cfg) };
        Ok((service::service_fn_ok(svc), Http::new()))
    }
}

// TODO: implement service_fn

impl<S, O, C, Transport, Action, Srv, H> IteratedCfgHelper<S, O, C, Action>
    for HyperServer<Transport>
where
    S: Borrow<ArcSwap<C>> + Sync + Send + 'static,
    for<'de> C: Deserialize<'de> + Send + Sync + 'static,
    O: Debug + StructOpt + Sync + Send + 'static,
    Transport: ResourceMaker<S, O, C, ()>,
    Transport::Resource: AsyncRead + AsyncWrite + Send + 'static,
    Action: ConnAction<S, O, C, Transport::ExtraCfg> + Sync + Send + 'static,
    Action::IntoFuture: IntoFuture<Item = (Srv, H), Error = FailError>,
    <Action::IntoFuture as IntoFuture>::Future: Send + 'static,
    Srv: Service<ReqBody = Body> + Send + 'static,
    Srv::Future: Send,
    H: Borrow<Http> + Send + 'static,
{
    fn apply<Extractor, ExtractedIter, Name>(
        mut extractor: Extractor,
        action: Action,
        name: Name,
        builder: Builder<S, O, C>,
    ) -> Builder<S, O, C>
    where
        Extractor: FnMut(&C) -> ExtractedIter + Send + 'static,
        ExtractedIter: IntoIterator<Item = Self>,
        Name: Clone + Display + Send + Sync + 'static,
    {
        let (shutdown_send, shutdown_recv) = oneshot::channel::<()>();
        let shutdown_recv = shutdown_recv.shared();
        let inner_action = move |spirit: &_, resource, extra_cfg: &_, _: &()| {
            let shutdown_recv = shutdown_recv.clone();
            action
                .action(spirit, extra_cfg)
                .into_future()
                .and_then(|(srv, http)| {
                    let conn = http.borrow().serve_connection(resource, srv);
                    let conn = ShutdownConn {
                        shutdown: Some(shutdown_recv),
                        conn,
                    };
                    conn.map_err(FailError::from)
                })
        };
        let inner_extractor = move |cfg: &_| {
            extractor(cfg)
                .into_iter()
                .map(|instance| (instance.transport, ()))
        };
        let mut shutdown_send = Some(shutdown_send);
        Transport::apply(inner_extractor, inner_action, name, builder).on_terminate(move || {
            if let Some(send) = shutdown_send.take() {
                let _ = send.send(());
            }
        })
    }
}

impl<S, O, C, Transport, Action, Srv, H> CfgHelper<S, O, C, Action> for HyperServer<Transport>
where
    S: Borrow<ArcSwap<C>> + Sync + Send + 'static,
    for<'de> C: Deserialize<'de> + Send + Sync + 'static,
    O: Debug + StructOpt + Sync + Send + 'static,
    Transport: ResourceMaker<S, O, C, ()>,
    Transport::Resource: AsyncRead + AsyncWrite + Send + 'static,
    Action: ConnAction<S, O, C, Transport::ExtraCfg> + Sync + Send + 'static,
    Action::IntoFuture: IntoFuture<Item = (Srv, H), Error = FailError>,
    <Action::IntoFuture as IntoFuture>::Future: Send + 'static,
    Srv: Service<ReqBody = Body> + Send + 'static,
    Srv::Future: Send,
    H: Borrow<Http> + Send + 'static,
{
    fn apply<Extractor, Name>(
        mut extractor: Extractor,
        action: Action,
        name: Name,
        builder: Builder<S, O, C>,
    ) -> Builder<S, O, C>
    where
        Extractor: FnMut(&C) -> Self + Send + 'static,
        Name: Clone + Display + Send + Sync + 'static,
    {
        let extractor = move |cfg: &_| iter::once(extractor(cfg));
        <Self as IteratedCfgHelper<S, O, C, Action>>::apply(extractor, action, name, builder)
    }
}