Skip to content

Commit

Permalink
feat: support grpc-web and add web feature
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Dec 1, 2023
1 parent 96c65bd commit bc7f32c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions console-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ keywords = [
default = ["env-filter"]
parking_lot = ["parking_lot_crate", "tracing-subscriber/parking_lot"]
env-filter = ["tracing-subscriber/env-filter"]
web = ["tonic-web"]

[dependencies]

Expand All @@ -53,6 +54,9 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
crossbeam-channel = "0.5"

# Only for the web feature:
tonic-web = { version = "0.10.2", optional = true }

[dev-dependencies]
tokio = { version = "^1.21", features = ["full", "rt-multi-thread"] }
tower = { version = "0.4", default-features = false }
Expand All @@ -61,3 +65,7 @@ futures = "0.3"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[[example]]
name = "app"
required-features = ["web"]
10 changes: 8 additions & 2 deletions console-subscriber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ impl Server {
/// ```
/// [`serve_with`]: Server::serve_with
pub async fn serve(self) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
self.serve_with(tonic::transport::Server::default()).await
self.serve_with(tonic::transport::Server::builder()).await
}

/// Starts the gRPC service with the given [`tonic`] gRPC transport server
Expand All @@ -937,14 +937,20 @@ impl Server {
/// [`tonic`]: https://docs.rs/tonic/
pub async fn serve_with(
self,
mut builder: tonic::transport::Server,
#[cfg(feature = "web")] builder: tonic::transport::Server,
#[cfg(not(feature = "web"))] mut builder: tonic::transport::Server,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let addr = self.addr.clone();
let ServerParts {
instrument_server,
aggregator,
} = self.into_parts();
let aggregate = spawn_named(aggregator.run(), "console::aggregate");
#[cfg(feature = "web")]
let router = builder
.accept_http1(true)
.add_service(tonic_web::enable(instrument_server));
#[cfg(not(feature = "web"))]
let router = builder.add_service(instrument_server);
let res = match addr {
ServerAddr::Tcp(addr) => {
Expand Down

0 comments on commit bc7f32c

Please sign in to comment.