Skip to content

Commit

Permalink
Update rest-grpc-multiplex example to include reflection (#1902)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn authored Apr 1, 2023
1 parent 0096fa6 commit 24f8dc5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
5 changes: 3 additions & 2 deletions examples/rest-grpc-multiplex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
9 changes: 8 additions & 1 deletion examples/rest-grpc-multiplex/build.rs
Original file line number Diff line number Diff line change
@@ -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();
}
12 changes: 11 additions & 1 deletion examples/rest-grpc-multiplex/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions examples/rest-grpc-multiplex/src/multiplex_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ where
A: Service<Request<Body>, Error = Infallible>,
A::Response: IntoResponse,
A::Future: Send + 'static,
B: Service<Request<Body>, Error = Infallible>,
B: Service<Request<Body>>,
B::Response: IntoResponse,
B::Future: Send + 'static,
{
type Response = Response<BoxBody>;
type Error = Infallible;
type Error = B::Error;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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())
})
}
Expand Down

0 comments on commit 24f8dc5

Please sign in to comment.