chore: Reorganize examples and interop crates (#180)

* chore: Reorganize examples and interop crates

* fix interop tests
This commit is contained in:
Lucio Franco
2019-12-12 11:53:15 -05:00
committed by GitHub
parent f096a238ac
commit d9a481baef
69 changed files with 25 additions and 22 deletions
+51
View File
@@ -0,0 +1,51 @@
pub mod pb {
tonic::include_proto!("grpc.examples.echo");
}
use futures::Stream;
use pb::{EchoRequest, EchoResponse};
use std::pin::Pin;
use tonic::transport::{Certificate, Identity, Server, ServerTlsConfig};
use tonic::{Request, Response, Status};
type EchoResult<T> = Result<Response<T>, Status>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
#[derive(Default)]
pub struct EchoServer;
#[tonic::async_trait]
impl pb::echo_server::Echo for EchoServer {
async fn unary_echo(&self, request: Request<EchoRequest>) -> EchoResult<EchoResponse> {
let message = request.into_inner().message;
Ok(Response::new(EchoResponse { message }))
}
type ServerStreamingEchoStream = ResponseStream;
type BidirectionalStreamingEchoStream = ResponseStream;
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let cert = tokio::fs::read("tonic-examples/data/tls/server.pem").await?;
let key = tokio::fs::read("tonic-examples/data/tls/server.key").await?;
let server_identity = Identity::from_pem(cert, key);
let client_ca_cert = tokio::fs::read("tonic-examples/data/tls/client_ca.pem").await?;
let client_ca_cert = Certificate::from_pem(client_ca_cert);
let addr = "[::1]:50051".parse().unwrap();
let server = EchoServer::default();
let tls = ServerTlsConfig::with_rustls()
.identity(server_identity)
.client_ca_root(client_ca_cert);
Server::builder()
.tls_config(tls)
.add_service(pb::echo_server::EchoServer::new(server))
.serve(addr)
.await?;
Ok(())
}