11 releases (6 breaking)

0.7.1 Oct 28, 2020
0.6.0 Oct 19, 2019
0.5.2 Mar 8, 2019
0.4.1 Dec 26, 2018
0.2.0 Oct 15, 2018

#7 in #spirit

Download history 29/week @ 2020-08-10 37/week @ 2020-08-17 50/week @ 2020-08-24 31/week @ 2020-08-31 72/week @ 2020-09-07 24/week @ 2020-09-14 18/week @ 2020-09-21 13/week @ 2020-09-28 37/week @ 2020-10-05 11/week @ 2020-10-12 73/week @ 2020-10-19 34/week @ 2020-10-26 10/week @ 2020-11-02 16/week @ 2020-11-09 26/week @ 2020-11-16 30/week @ 2020-11-23

158 downloads per month
Used in spirit

Apache-2.0 OR MIT

370KB
5K SLoC

Spirit-hyper

Travis Build Status

Several helpers to easily integrate hyper with configuration managed by the spirit system.

See the docs and the examples.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

[Spirit][spirit] extension for Hyper servers.

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...).

Tokio runtime

This uses the [spirit_tokio] crate under the hood. Similar drawback with initializing a runtime applies here too (see the [spirit_tokio] docs for details).

Examples

use hyper::{Body, Request, Response};
use serde::Deserialize;
use spirit::{Empty, Pipeline, Spirit};
use spirit::prelude::*;
use spirit_hyper::{server_from_handler, BuildServer, HttpServer};

const DEFAULT_CONFIG: &str = r#"
[server]
port = 2234
"#;

#[derive(Default, Deserialize)]
struct Config {
    server: HttpServer,
}

impl Config {
    fn server(&self) -> HttpServer {
        self.server.clone()
    }
}

async fn request(_req: Request<Body>) -> Response<Body> {
    Response::new(Body::from("Hello world\n"))
}

fn main() {
    Spirit::<Empty, Config>::new()
        .config_defaults(DEFAULT_CONFIG)
        .with(
            // Let's build a http server as configured by the user
            Pipeline::new("listen")
                .extract_cfg(Config::server)
                // This is where we teach the server what it serves. It is the usual stuff from
                // hyper.
                .transform(BuildServer(server_from_handler(request)))
        )
        .run(|spirit| {
#           let spirit = std::sync::Arc::clone(spirit);
#           std::thread::spawn(move || spirit.terminate());
            Ok(())
        });
}

Further examples (with more flexible handling) are in the git repository.

Dependencies

~10MB
~181K SLoC