The UnixListenerExt trait extends tokio::net::UnixListener to allow provisioning a hyper HTTP server over a Unix domain socket.
By calling .serve() on a UnixListener, you can indefinitely accept and respond to incoming connections. The method requires a factory function that generates a response handler for each connection.
Note on OS Compatibility: The implementation internally disables keep_alive on the HTTP/1 builder to prevent blocking and E_NOTCONN errors on macOS (OSX).
use hyper::Response;
use hyperlocal::UnixListenerExt;
use tokio::net::UnixListener;
let future = async move {
let listener = UnixListener::bind("/tmp/hyperlocal.sock").expect("parsed unix path");
listener
.serve(|| {
|_request| async {
Ok::<_, hyper::Error>(Response::new("Hello, world.".to_string()))
}
})
.await
.expect("failed to serve a connection")
};
future.await;