Add support of Unix socket
This commit is contained in:
parent
7a9b9c0f09
commit
368d5eca46
4 changed files with 59 additions and 10 deletions
|
@ -1,13 +1,28 @@
|
|||
use hello_world::greeter_client::GreeterClient;
|
||||
use hello_world::HelloRequest;
|
||||
#![cfg_attr(not(unix), allow(unused_imports))]
|
||||
|
||||
pub mod hello_world {
|
||||
tonic::include_proto!("helloworld");
|
||||
}
|
||||
|
||||
use hello_world::{greeter_client::GreeterClient, HelloRequest};
|
||||
#[cfg(unix)]
|
||||
use tokio::net::UnixStream;
|
||||
use tonic::transport::{Endpoint, Uri};
|
||||
use tower::service_fn;
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut client = GreeterClient::connect("http://[::1]:50051").await?;
|
||||
let channel = Endpoint::try_from("http://[::]:50051")?
|
||||
.connect_with_connector(service_fn(|_: Uri| {
|
||||
let path = "helloworld.sock";
|
||||
|
||||
// Connect to a Uds socket
|
||||
UnixStream::connect(path)
|
||||
}))
|
||||
.await?;
|
||||
|
||||
let mut client = GreeterClient::new(channel);
|
||||
|
||||
let request = tonic::Request::new(HelloRequest {
|
||||
name: "Tonic".into(),
|
||||
|
@ -18,4 +33,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
println!("RESPONSE={:?}", response);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn main() {
|
||||
panic!("The `uds` example only works on unix systems!");
|
||||
}
|
Reference in a new issue