1 pub mod pb { 2 tonic::include_proto!("/grpc.examples.unaryecho"); 3 } 4 5 use pb::{echo_client::EchoClient, EchoRequest}; 6 use tonic::transport::{Certificate, Channel, ClientTlsConfig}; 7 8 #[tokio::main] main() -> Result<(), Box<dyn std::error::Error>>9async fn main() -> Result<(), Box<dyn std::error::Error>> { 10 let data_dir = std::path::PathBuf::from_iter([std::env!("CARGO_MANIFEST_DIR"), "data"]); 11 let pem = std::fs::read_to_string(data_dir.join("tls/ca.pem"))?; 12 let ca = Certificate::from_pem(pem); 13 14 let tls = ClientTlsConfig::new() 15 .ca_certificate(ca) 16 .domain_name("example.com"); 17 18 let channel = Channel::from_static("https://[::1]:50051") 19 .tls_config(tls)? 20 .connect() 21 .await?; 22 23 let mut client = EchoClient::new(channel); 24 let request = tonic::Request::new(EchoRequest { 25 message: "hello".into(), 26 }); 27 28 let response = client.unary_echo(request).await?; 29 30 println!("RESPONSE={:?}", response); 31 32 Ok(()) 33 } 34