Update rest-grpc-multiplex example to include reflection (#1902)

This commit is contained in:
David Pedersen 2023-04-01 20:49:36 +02:00 committed by GitHub
parent 0096fa6e1e
commit 24f8dc53f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 8 deletions

View file

@ -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"] }

View file

@ -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();
}

View file

@ -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);

View file

@ -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>> {
@ -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())
})
}