Created
April 5, 2019 01:44
-
-
Save david415/bb9918f199c009dabdd9e64ce889a63d to your computer and use it in GitHub Desktop.
rust hyperlocal attempt at low level server unix socket listener.. fail not working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | |
) | |
}) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment