Files
tonic/examples/src/tls_client_auth/client.rs
T
Thomas MayandLucio Franco 59d008db7e chore(docs): Fix examples link (#202)
* url is just examples, not tonic-examples

* Change root directory from tonic-examples to examples.  Matches current directory structure.
2019-12-20 22:39:39 -05:00

38 lines
1.1 KiB
Rust

pub mod pb {
tonic::include_proto!("grpc.examples.echo");
}
use pb::{echo_client::EchoClient, EchoRequest};
use tonic::transport::{Certificate, Channel, ClientTlsConfig, Identity};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let server_root_ca_cert = tokio::fs::read("examples/data/tls/ca.pem").await?;
let server_root_ca_cert = Certificate::from_pem(server_root_ca_cert);
let client_cert = tokio::fs::read("examples/data/tls/client1.pem").await?;
let client_key = tokio::fs::read("examples/data/tls/client1.key").await?;
let client_identity = Identity::from_pem(client_cert, client_key);
let tls = ClientTlsConfig::new()
.domain_name("localhost")
.ca_certificate(server_root_ca_cert)
.identity(client_identity);
let channel = Channel::from_static("http://[::1]:50051")
.tls_config(tls)
.connect()
.await?;
let mut client = EchoClient::new(channel);
let request = tonic::Request::new(EchoRequest {
message: "hello".into(),
});
let response = client.unary_echo(request).await?;
println!("RESPONSE={:?}", response);
Ok(())
}