use poem::{ endpoint::StaticFilesEndpoint, listener::{Listener, TcpListener, UnixListener}, Route, Server, }; #[derive(clap::Parser)] pub struct Options { #[clap(long, env = "UDS", default_value = "/tmp/hello_unix.sock")] /// Set uds path uds: String, #[clap(long, env = "PORT", default_value = "8080")] /// Set tcp port port: String, } impl Options { fn port(&self) -> String { self.port.clone() } fn uds(&self) -> String { self.uds.clone() } fn addr(&self) -> String { format!("0.0.0.0:{}", self.port()).to_string() } pub async fn run(&mut self) -> Result<(), anyhow::Error> { let ser = StaticFilesEndpoint::new(".").show_files_listing(); let app = Route::new().at("*", ser); // remove old socket std::fs::remove_file(self.uds())?; dbg!(self.uds()); eprintln!("curl --unix-socket {} http://localhost/path", self.uds()); dbg!(self.addr()); let ln = { let ln_uds = UnixListener::bind(self.uds()); let ln_tcp = TcpListener::bind(self.addr()); ln_uds.combine(ln_tcp) }; Ok(Server::new(ln).name("hello").run(app).await?) } }