softprops / hyperlocal

🔌 ✨rustlang hyper bindings for local unix domain sockets
MIT License
229 stars 46 forks source link

low level example server is broken #22

Closed david415 closed 1 month ago

david415 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

Although I guess #20 is relevant here.

softprops commented 1 month ago

I just tested client and server examples on latest and everything worked