examples: Add tower-client example (#466)

This commit is contained in:
Lucio Franco
2020-09-29 11:16:07 -04:00
committed by GitHub
parent d4899df832
commit 9ea4a64a6c
2 changed files with 74 additions and 1 deletions
+5 -1
View File
@@ -70,6 +70,10 @@ path = "src/tls_client_auth/client.rs"
name = "tower-server"
path = "src/tower/server.rs"
[[bin]]
name = "tower-client"
path = "src/tower/client.rs"
[[bin]]
name = "multiplex-server"
path = "src/multiplex/server.rs"
@@ -163,4 +167,4 @@ tonic-health = { path = "../tonic-health" }
listenfd = "0.3"
[build-dependencies]
tonic-build = { path = "../tonic-build", features = ["prost"] }
tonic-build = { path = "../tonic-build", features = ["prost"] }
+69
View File
@@ -0,0 +1,69 @@
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use service::AuthSvc;
use tonic::transport::Channel;
pub mod hello_world {
tonic::include_proto!("helloworld");
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let channel = Channel::from_static("http://[::1]:50051").connect().await?;
let auth = AuthSvc::new(channel);
let mut client = GreeterClient::new(auth);
let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
});
let response = client.say_hello(request).await?;
println!("RESPONSE={:?}", response);
Ok(())
}
mod service {
use http::{Request, Response};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tonic::body::BoxBody;
use tonic::client::GrpcService;
use tonic::transport::Body;
use tonic::transport::Channel;
use tower::Service;
pub struct AuthSvc {
inner: Channel,
}
impl AuthSvc {
pub fn new(inner: Channel) -> Self {
AuthSvc { inner }
}
}
impl Service<Request<BoxBody>> for AuthSvc {
type Response = Response<Body>;
type Error = Box<dyn std::error::Error + Send + Sync>;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx).map_err(Into::into)
}
fn call(&mut self, req: Request<BoxBody>) -> Self::Future {
let mut channel = self.inner.clone();
Box::pin(async move {
// Do extra async work here...
channel.call(req).await.map_err(Into::into)
})
}
}
}