14 releases (8 breaking)

0.9.0 Apr 3, 2021
0.8.1 Jan 10, 2021
0.7.1 Oct 28, 2020
0.6.0 Oct 19, 2019
0.2.0 Oct 15, 2018

#5 in #spirit

Download history 2/week @ 2024-03-07 32/week @ 2024-03-28 6/week @ 2024-04-04

54 downloads per month
Used in spirit

Apache-2.0 OR MIT

435KB
5.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| {
            Ok(())
        });
}

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

Dependencies

~11–23MB
~285K SLoC