From 24f8dc53f49d8946418f103314ab6a31ff68be5c Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sat, 1 Apr 2023 20:49:36 +0200 Subject: [PATCH] Update `rest-grpc-multiplex` example to include reflection (#1902) --- examples/rest-grpc-multiplex/Cargo.toml | 5 +++-- examples/rest-grpc-multiplex/build.rs | 9 ++++++++- examples/rest-grpc-multiplex/src/main.rs | 12 +++++++++++- .../rest-grpc-multiplex/src/multiplex_service.rs | 8 ++++---- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/examples/rest-grpc-multiplex/Cargo.toml b/examples/rest-grpc-multiplex/Cargo.toml index 61afe01a..c0865f0c 100644 --- a/examples/rest-grpc-multiplex/Cargo.toml +++ b/examples/rest-grpc-multiplex/Cargo.toml @@ -10,10 +10,11 @@ futures = "0.3" hyper = { version = "0.14", features = ["full"] } prost = "0.11" tokio = { version = "1", features = ["full"] } -tonic = { version = "0.8" } +tonic = { version = "0.9" } +tonic-reflection = "0.9" tower = { version = "0.4", features = ["full"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } [build-dependencies] -tonic-build = { version = "0.8", features = ["prost"] } +tonic-build = { version = "0.9", features = ["prost"] } diff --git a/examples/rest-grpc-multiplex/build.rs b/examples/rest-grpc-multiplex/build.rs index fb515867..3b3886e3 100644 --- a/examples/rest-grpc-multiplex/build.rs +++ b/examples/rest-grpc-multiplex/build.rs @@ -1,3 +1,10 @@ +use std::{env, path::PathBuf}; + fn main() { - tonic_build::compile_protos("proto/helloworld.proto").unwrap(); + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); + + tonic_build::configure() + .file_descriptor_set_path(out_dir.join("helloworld_descriptor.bin")) + .compile(&["proto/helloworld.proto"], &["/proto"]) + .unwrap(); } diff --git a/examples/rest-grpc-multiplex/src/main.rs b/examples/rest-grpc-multiplex/src/main.rs index bd4ce0d9..f804f6ae 100644 --- a/examples/rest-grpc-multiplex/src/main.rs +++ b/examples/rest-grpc-multiplex/src/main.rs @@ -18,6 +18,9 @@ mod multiplex_service; mod proto { tonic::include_proto!("helloworld"); + + pub(crate) const FILE_DESCRIPTOR_SET: &[u8] = + tonic::include_file_descriptor_set!("helloworld_descriptor"); } #[derive(Default)] @@ -58,7 +61,14 @@ async fn main() { let rest = Router::new().route("/", get(web_root)); // build the grpc service - let grpc = GreeterServer::new(GrpcServiceImpl::default()); + let reflection_service = tonic_reflection::server::Builder::configure() + .register_encoded_file_descriptor_set(proto::FILE_DESCRIPTOR_SET) + .build() + .unwrap(); + let grpc = tonic::transport::Server::builder() + .add_service(reflection_service) + .add_service(GreeterServer::new(GrpcServiceImpl::default())) + .into_service(); // combine them into one service let service = MultiplexService::new(rest, grpc); diff --git a/examples/rest-grpc-multiplex/src/multiplex_service.rs b/examples/rest-grpc-multiplex/src/multiplex_service.rs index de6b8b6c..0f08dbf6 100644 --- a/examples/rest-grpc-multiplex/src/multiplex_service.rs +++ b/examples/rest-grpc-multiplex/src/multiplex_service.rs @@ -46,12 +46,12 @@ where A: Service, Error = Infallible>, A::Response: IntoResponse, A::Future: Send + 'static, - B: Service, Error = Infallible>, + B: Service>, B::Response: IntoResponse, B::Future: Send + 'static, { type Response = Response; - type Error = Infallible; + type Error = B::Error; type Future = BoxFuture<'static, Result>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { @@ -62,7 +62,7 @@ where return Ok(()).into(); } (false, _) => { - ready!(self.rest.poll_ready(cx))?; + ready!(self.rest.poll_ready(cx)).map_err(|err| match err {})?; self.rest_ready = true; } (_, false) => { @@ -98,7 +98,7 @@ where self.rest_ready = false; let future = self.rest.call(req); Box::pin(async move { - let res = future.await?; + let res = future.await.map_err(|err| match err {})?; Ok(res.into_response()) }) }