diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 5c3e7e8b5..0274f1cbd 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -287,7 +287,7 @@ grpc-web = ["dep:tonic-web", "dep:bytes", "dep:http", "dep:hyper", "dep:tracing- tracing = ["dep:tracing", "dep:tracing-subscriber"] hyper-warp = ["dep:either", "dep:tower", "dep:hyper", "dep:http", "dep:http-body", "dep:warp"] hyper-warp-multiplex = ["hyper-warp"] -uds = ["tokio-stream/net", "dep:tower", "dep:hyper"] +uds = ["tokio-stream/net", "dep:tower", "dep:hyper", "dep:ustr"] streaming = ["tokio-stream", "dep:h2"] mock = ["tokio-stream", "dep:tower"] tower = ["dep:hyper", "dep:tower", "dep:http"] @@ -337,6 +337,7 @@ tokio-rustls = { version = "0.24.0", optional = true } hyper-rustls = { version = "0.24.0", features = ["http2"], optional = true } rustls-pemfile = { version = "1", optional = true } tower-http = { version = "0.4", optional = true } +ustr = { version = "1.0.0", optional = true } [build-dependencies] tonic-build = { path = "../tonic-build", features = ["prost"] } diff --git a/examples/src/uds/client.rs b/examples/src/uds/client.rs index e78531ac4..6e3ee176b 100644 --- a/examples/src/uds/client.rs +++ b/examples/src/uds/client.rs @@ -5,21 +5,28 @@ pub mod hello_world { } use hello_world::{greeter_client::GreeterClient, HelloRequest}; +use std::env; #[cfg(unix)] use tokio::net::UnixStream; use tonic::transport::{Endpoint, Uri}; use tower::service_fn; +use ustr::ustr; #[cfg(unix)] #[tokio::main] async fn main() -> Result<(), Box> { + // Path needs to be a static str due to service_fn FnMut. + let path = match env::var("EXAMPLE_UDS_PATH") { + Ok(path) => ustr(path.as_ref()).as_str(), + Err(_) => "/tmp/tonic/helloworld" + }; + println!("Using path {}", path); + // We will ignore this uri because uds do not use it // if your connector does use the uri it will be provided // as the request to the `MakeConnection`. let channel = Endpoint::try_from("http://[::]:50051")? - .connect_with_connector(service_fn(|_: Uri| { - let path = "/tmp/tonic/helloworld"; - + .connect_with_connector(service_fn(move |_: Uri| { // Connect to a Uds socket UnixStream::connect(path) })) diff --git a/examples/src/uds/server.rs b/examples/src/uds/server.rs index ccf9c91a8..c8c76a998 100644 --- a/examples/src/uds/server.rs +++ b/examples/src/uds/server.rs @@ -1,6 +1,6 @@ #![cfg_attr(not(unix), allow(unused_imports))] -use std::path::Path; +use std::{env, path::Path}; #[cfg(unix)] use tokio::net::UnixListener; #[cfg(unix)] @@ -43,9 +43,9 @@ impl Greeter for MyGreeter { #[cfg(unix)] #[tokio::main] async fn main() -> Result<(), Box> { - let path = "/tmp/tonic/helloworld"; - - std::fs::create_dir_all(Path::new(path).parent().unwrap())?; + let path = env::var("EXAMPLE_UDS_PATH").unwrap_or("/tmp/tonic/helloworld".to_owned()); + std::fs::create_dir_all(Path::new(&path).parent().unwrap())?; + println!("Using path {}", path); let greeter = MyGreeter::default();