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