Open
Description
I do not know why I spent all this morning for just open a unix socket, anyway, the full example ported from TcpListener to UnixListener:
use bytes::Bytes;
use http_body_util::Full;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper::{Request, Response};
use tokio::net::{TcpListener,UnixListener};
use hyper_util::rt::TokioIo;
use std::convert::Infallible;
async fn hello(_: Request<hyper::body::Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
Ok(Response::new(Full::new(Bytes::from("Hello, World!"))))
}
#[tokio::main]
async fn main() {
let socketpath = "/tmp/notexistent.sock";
let path = std::path::Path::new(socketpath);
if path.exists() {
tokio::fs::remove_file(path).await.expect("Could not remove old socket!");
}
let listener = UnixListener::bind(path).unwrap();
//let listener = TcpListener::bind(addr).await.unwrap();
loop {
let (stream, _) = listener.accept().await.unwrap();
let io = TokioIo::new(stream);
tokio::task::spawn(async move {
if let Err(err) = // http1::Builder::new()
http1::Builder::new().serve_connection(
io,
service_fn(hello),
)
.await
{
println!("Failed to serve connection: {:?}", err);
}
});
}
}
try with:
curl --no-buffer -XGET --unix-socket /tmp/notexistent.sock http://domain/saysomething
I refer to the example in https://hyper.rs/guides/1/server/hello-world/
Metadata
Metadata
Assignees
Labels
No labels
Activity
danielecr commentedon Nov 23, 2023
hyperium/hyper#3440
let's see
bheesham commentedon Dec 19, 2023
fwiw there's a client+server example here: https://github.com/tokio-rs/axum/blob/main/examples/unix-domain-socket/src/main.rs
SpeedyCraftah commentedon Mar 8, 2025
Example works! But please make sure you are using the latest version of Axum, older Axum doesn't accept a UnixListener in axum::serve. Took up a few hours of my morning to figure that out 😅also a good reminder for me to update my packages every once a while.