Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

low level example server is broken #22

Open
david415 opened this issue Apr 5, 2019 · 2 comments
Open

low level example server is broken #22

david415 opened this issue Apr 5, 2019 · 2 comments

Comments

@david415
Copy link

david415 commented Apr 5, 2019

I'd like to be able to use the lower level api for servers as is described in this source code comment: https://github.com/softprops/hyperlocal/blob/master/src/server/mod.rs#L163-L207

Below i added a main function for that example code.... and.... it doesn't work. It compiles. It runs and exits immediately.

extern crate hyper;
extern crate hyperlocal;

use std::os::unix::net::UnixListener;
use hyper::{Response, rt::{Future, Stream}, service::service_fn};
use hyperlocal::server::{Http, Incoming};


fn main() {
    if let Err(err) =  std::fs::remove_file("hyperlocal_test_echo_server_2.sock") {
        if err.kind() != std::io::ErrorKind::NotFound {
            panic!("{}", err);
        }
    }

    let listener = UnixListener::bind("hyperlocal_test_echo_server_2.sock").unwrap();
    let incoming = Incoming::from_std(listener, &Default::default()).unwrap();
    let serve = Http::new().serve_incoming(
        incoming,
        move || service_fn(
            |req| Ok::<_, hyper::Error>(Response::new(req.into_body()))
        )
    );

    let server = serve.for_each(|connecting| {
        connecting
            .then(|connection| {
                let connection = connection.unwrap();
                Ok::<_, hyper::Error>(connection)
            })
            .flatten()
            .map_err(|err| {
                std::io::Error::new(
                    std::io::ErrorKind::Other,
                    format!("failed to serve connection: {}", err),
                )
            })
    });
}
@samscott89
Copy link

I suspect you'll need to add tokio::run(server). as the last line. The serve.for_each(...) part is creating a new Stream, which needs to be spawned on a runtime: https://tokio.rs/docs/getting-started/hello-world/

(I agree though, that the example should include that line)

@samscott89
Copy link

Although I guess #20 is relevant here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants