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, Identity};
7
8 #[tokio::main]
main() -> Result<(), Box<dyn std::error::Error>>9 async 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 server_root_ca_cert = std::fs::read_to_string(data_dir.join("tls/ca.pem"))?;
12 let server_root_ca_cert = Certificate::from_pem(server_root_ca_cert);
13 let client_cert = std::fs::read_to_string(data_dir.join("tls/client1.pem"))?;
14 let client_key = std::fs::read_to_string(data_dir.join("tls/client1.key"))?;
15 let client_identity = Identity::from_pem(client_cert, client_key);
16
17 let tls = ClientTlsConfig::new()
18 .domain_name("localhost")
19 .ca_certificate(server_root_ca_cert)
20 .identity(client_identity);
21
22 let channel = Channel::from_static("https://[::1]:50051")
23 .tls_config(tls)?
24 .connect()
25 .await?;
26
27 let mut client = EchoClient::new(channel);
28
29 let request = tonic::Request::new(EchoRequest {
30 message: "hello".into(),
31 });
32
33 let response = client.unary_echo(request).await?;
34
35 println!("RESPONSE={:?}", response);
36
37 Ok(())
38 }
39