Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work greetly with axum, but not required in hyper 1.0 #64

Open
danielecr opened this issue Nov 23, 2023 · 2 comments
Open

Work greetly with axum, but not required in hyper 1.0 #64

danielecr opened this issue Nov 23, 2023 · 2 comments

Comments

@danielecr
Copy link

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/

@danielecr
Copy link
Author

hyperium/hyper#3440
let's see

@bheesham
Copy link

fwiw there's a client+server example here: https://github.com/tokio-rs/axum/blob/main/examples/unix-domain-socket/src/main.rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants