mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-21 22:56:46 +01:00
Update rest-grpc-multiplex
example to include reflection (#1902)
This commit is contained in:
parent
0096fa6e1e
commit
24f8dc53f4
4 changed files with 26 additions and 8 deletions
|
@ -10,10 +10,11 @@ futures = "0.3"
|
||||||
hyper = { version = "0.14", features = ["full"] }
|
hyper = { version = "0.14", features = ["full"] }
|
||||||
prost = "0.11"
|
prost = "0.11"
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
tonic = { version = "0.8" }
|
tonic = { version = "0.9" }
|
||||||
|
tonic-reflection = "0.9"
|
||||||
tower = { version = "0.4", features = ["full"] }
|
tower = { version = "0.4", features = ["full"] }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
tonic-build = { version = "0.8", features = ["prost"] }
|
tonic-build = { version = "0.9", features = ["prost"] }
|
||||||
|
|
|
@ -1,3 +1,10 @@
|
||||||
|
use std::{env, path::PathBuf};
|
||||||
|
|
||||||
fn main() {
|
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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,9 @@ mod multiplex_service;
|
||||||
|
|
||||||
mod proto {
|
mod proto {
|
||||||
tonic::include_proto!("helloworld");
|
tonic::include_proto!("helloworld");
|
||||||
|
|
||||||
|
pub(crate) const FILE_DESCRIPTOR_SET: &[u8] =
|
||||||
|
tonic::include_file_descriptor_set!("helloworld_descriptor");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -58,7 +61,14 @@ async fn main() {
|
||||||
let rest = Router::new().route("/", get(web_root));
|
let rest = Router::new().route("/", get(web_root));
|
||||||
|
|
||||||
// build the grpc service
|
// 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
|
// combine them into one service
|
||||||
let service = MultiplexService::new(rest, grpc);
|
let service = MultiplexService::new(rest, grpc);
|
||||||
|
|
|
@ -46,12 +46,12 @@ where
|
||||||
A: Service<Request<Body>, Error = Infallible>,
|
A: Service<Request<Body>, Error = Infallible>,
|
||||||
A::Response: IntoResponse,
|
A::Response: IntoResponse,
|
||||||
A::Future: Send + 'static,
|
A::Future: Send + 'static,
|
||||||
B: Service<Request<Body>, Error = Infallible>,
|
B: Service<Request<Body>>,
|
||||||
B::Response: IntoResponse,
|
B::Response: IntoResponse,
|
||||||
B::Future: Send + 'static,
|
B::Future: Send + 'static,
|
||||||
{
|
{
|
||||||
type Response = Response<BoxBody>;
|
type Response = Response<BoxBody>;
|
||||||
type Error = Infallible;
|
type Error = B::Error;
|
||||||
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
|
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
@ -62,7 +62,7 @@ where
|
||||||
return Ok(()).into();
|
return Ok(()).into();
|
||||||
}
|
}
|
||||||
(false, _) => {
|
(false, _) => {
|
||||||
ready!(self.rest.poll_ready(cx))?;
|
ready!(self.rest.poll_ready(cx)).map_err(|err| match err {})?;
|
||||||
self.rest_ready = true;
|
self.rest_ready = true;
|
||||||
}
|
}
|
||||||
(_, false) => {
|
(_, false) => {
|
||||||
|
@ -98,7 +98,7 @@ where
|
||||||
self.rest_ready = false;
|
self.rest_ready = false;
|
||||||
let future = self.rest.call(req);
|
let future = self.rest.call(req);
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let res = future.await?;
|
let res = future.await.map_err(|err| match err {})?;
|
||||||
Ok(res.into_response())
|
Ok(res.into_response())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue