-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: fix gRPC multiplex example #2825
Open
abhiaagarwal
wants to merge
1
commit into
tokio-rs:main
Choose a base branch
from
abhiaagarwal:grpc_multiplex
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+79
−190
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,84 +4,90 @@ | |
//! cargo run -p example-rest-grpc-multiplex | ||
//! ``` | ||
|
||
// TODO | ||
fn main() { | ||
eprint!("this example has not yet been updated to hyper 1.0"); | ||
} | ||
use axum::{extract::Request, http::header::CONTENT_TYPE, routing::get, Router}; | ||
use proto::{ | ||
greeter_server::{Greeter, GreeterServer}, | ||
HelloReply, HelloRequest, | ||
}; | ||
use std::net::SocketAddr; | ||
use tonic::{Request as TonicRequest, Response as TonicResponse, Status}; | ||
use tower::{steer::Steer, make::Shared}; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
|
||
// use self::multiplex_service::MultiplexService; | ||
// use axum::{routing::get, Router}; | ||
// use proto::{ | ||
// greeter_server::{Greeter, GreeterServer}, | ||
// HelloReply, HelloRequest, | ||
// }; | ||
// use std::net::SocketAddr; | ||
// use tonic::{Response as TonicResponse, Status}; | ||
// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
mod proto { | ||
tonic::include_proto!("helloworld"); | ||
|
||
// mod multiplex_service; | ||
pub(crate) const FILE_DESCRIPTOR_SET: &[u8] = | ||
tonic::include_file_descriptor_set!("helloworld_descriptor"); | ||
} | ||
|
||
// mod proto { | ||
// tonic::include_proto!("helloworld"); | ||
#[derive(Default)] | ||
struct GrpcServiceImpl {} | ||
|
||
// pub(crate) const FILE_DESCRIPTOR_SET: &[u8] = | ||
// tonic::include_file_descriptor_set!("helloworld_descriptor"); | ||
// } | ||
#[tonic::async_trait] | ||
impl Greeter for GrpcServiceImpl { | ||
async fn say_hello( | ||
&self, | ||
request: TonicRequest<HelloRequest>, | ||
) -> Result<TonicResponse<HelloReply>, Status> { | ||
tracing::info!("Got a gRPC request from {:?}", request.remote_addr()); | ||
|
||
// #[derive(Default)] | ||
// struct GrpcServiceImpl {} | ||
let reply = HelloReply { | ||
message: format!("Hello {}!", request.into_inner().name), | ||
}; | ||
|
||
// #[tonic::async_trait] | ||
// impl Greeter for GrpcServiceImpl { | ||
// async fn say_hello( | ||
// &self, | ||
// request: tonic::Request<HelloRequest>, | ||
// ) -> Result<TonicResponse<HelloReply>, Status> { | ||
// tracing::info!("Got a request from {:?}", request.remote_addr()); | ||
Ok(TonicResponse::new(reply)) | ||
} | ||
} | ||
|
||
// let reply = HelloReply { | ||
// message: format!("Hello {}!", request.into_inner().name), | ||
// }; | ||
async fn web_root() -> &'static str { | ||
tracing::info!("Got a REST request"); | ||
|
||
// Ok(TonicResponse::new(reply)) | ||
// } | ||
// } | ||
"Hello, World!" | ||
} | ||
|
||
// async fn web_root() -> &'static str { | ||
// "Hello, World!" | ||
// } | ||
#[tokio::main] | ||
async fn main() { | ||
// initialize tracing | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::EnvFilter::try_from_default_env() | ||
.unwrap_or_else(|_| "example_rest_grpc_multiplex=debug".into()), | ||
) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
|
||
// #[tokio::main] | ||
// async fn main() { | ||
// // initialize tracing | ||
// tracing_subscriber::registry() | ||
// .with( | ||
// tracing_subscriber::EnvFilter::try_from_default_env() | ||
// .unwrap_or_else(|_| "example_rest_grpc_multiplex=debug".into()), | ||
// ) | ||
// .with(tracing_subscriber::fmt::layer()) | ||
// .init(); | ||
// build the rest service | ||
let rest = Router::new().route("/", get(web_root)); | ||
|
||
// // build the rest service | ||
// let rest = Router::new().route("/", get(web_root)); | ||
// build the grpc service | ||
let reflection_service = tonic_reflection::server::Builder::configure() | ||
.register_encoded_file_descriptor_set(proto::FILE_DESCRIPTOR_SET) | ||
.build() | ||
.unwrap(); | ||
|
||
// // build the grpc service | ||
// 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(); | ||
let grpc = tonic::transport::Server::builder() | ||
.add_service(reflection_service) | ||
.add_service(GreeterServer::new(GrpcServiceImpl::default())) | ||
.into_router(); | ||
|
||
// // combine them into one service | ||
// let service = MultiplexService::new(rest, grpc); | ||
// combine them into one service | ||
let service = Steer::new(vec![rest, grpc], |req: &Request, _services: &[_]| { | ||
if req | ||
.headers() | ||
.get(CONTENT_TYPE) | ||
.map(|content_type| content_type.as_bytes()) | ||
.filter(|content_type| content_type.starts_with(b"application/grpc")) | ||
.is_some() | ||
Comment on lines
+79
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit, you could do just (Although now I see you have just copied this from the deleted file) |
||
{ | ||
1 | ||
} else { | ||
0 | ||
} | ||
}); | ||
|
||
// let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
// tracing::debug!("listening on {}", addr); | ||
// hyper::Server::bind(&addr) | ||
// .serve(tower::make::Shared::new(service)) | ||
// .await | ||
// .unwrap(); | ||
// } | ||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); | ||
tracing::debug!("listening on {}", addr); | ||
axum::serve(listener, Shared::new(service)).await.unwrap(); | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't want examples within this repo to pull in a crates.io release of axum. I'm considering moving examples to a separate repo anyways though, see #2942 for discussion.