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
+32
View File
@@ -0,0 +1,32 @@
pub mod pb {
tonic::include_proto!("/grpc.examples.echo");
}
use pb::{echo_client::EchoClient, EchoRequest};
use tonic::transport::{Certificate, Channel, ClientTlsConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let pem = tokio::fs::read("tonic-examples/data/tls/ca.pem").await?;
let ca = Certificate::from_pem(pem);
let tls = ClientTlsConfig::with_rustls()
.ca_certificate(ca)
.domain_name("example.com");
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(())
}
+69
View File
@@ -0,0 +1,69 @@
pub mod pb {
tonic::include_proto!("/grpc.examples.echo");
}
use futures::Stream;
use pb::{EchoRequest, EchoResponse};
use std::pin::Pin;
use tonic::{
transport::{Identity, Server, ServerTlsConfig},
Request, Response, Status, Streaming,
};
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;
async fn server_streaming_echo(
&self,
_: Request<EchoRequest>,
) -> EchoResult<Self::ServerStreamingEchoStream> {
Err(Status::unimplemented("not implemented"))
}
async fn client_streaming_echo(
&self,
_: Request<Streaming<EchoRequest>>,
) -> EchoResult<EchoResponse> {
Err(Status::unimplemented("not implemented"))
}
type BidirectionalStreamingEchoStream = ResponseStream;
async fn bidirectional_streaming_echo(
&self,
_: Request<Streaming<EchoRequest>>,
) -> EchoResult<Self::BidirectionalStreamingEchoStream> {
Err(Status::unimplemented("not implemented"))
}
}
#[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 identity = Identity::from_pem(cert, key);
let addr = "[::1]:50051".parse().unwrap();
let server = EchoServer::default();
Server::builder()
.tls_config(ServerTlsConfig::with_rustls().identity(identity))
.add_service(pb::echo_server::EchoServer::new(server))
.serve(addr)
.await?;
Ok(())
}