diff --git a/Cargo.lock b/Cargo.lock index 46290ab97..0385dce8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -336,6 +336,7 @@ dependencies = [ "tokio", "tokio-stream", "tonic", + "tonic-web", "tower", "tracing", "tracing-core", @@ -742,6 +743,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-range-header" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" + [[package]] name = "httparse" version = "1.8.0" @@ -1856,6 +1863,26 @@ dependencies = [ "syn 2.0.33", ] +[[package]] +name = "tonic-web" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fddb2a37b247e6adcb9f239f4e5cefdcc5ed526141a416b943929f13aea2cce" +dependencies = [ + "base64 0.21.0", + "bytes", + "http", + "http-body", + "hyper", + "pin-project", + "tokio-stream", + "tonic", + "tower-http", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "tower" version = "0.4.13" @@ -1876,6 +1903,24 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" +dependencies = [ + "bitflags 2.4.0", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-range-header", + "pin-project-lite", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-layer" version = "0.3.2" diff --git a/console-subscriber/Cargo.toml b/console-subscriber/Cargo.toml index 113a4ceb3..306e6e2d4 100644 --- a/console-subscriber/Cargo.toml +++ b/console-subscriber/Cargo.toml @@ -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] @@ -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 } @@ -61,3 +65,7 @@ futures = "0.3" [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] + +[[example]] +name = "app" +required-features = ["web"] diff --git a/console-subscriber/src/lib.rs b/console-subscriber/src/lib.rs index fde18af8d..fcd9f8457 100644 --- a/console-subscriber/src/lib.rs +++ b/console-subscriber/src/lib.rs @@ -922,7 +922,7 @@ impl Server { /// ``` /// [`serve_with`]: Server::serve_with pub async fn serve(self) -> Result<(), Box> { - 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 @@ -937,7 +937,8 @@ 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> { let addr = self.addr.clone(); let ServerParts { @@ -945,6 +946,11 @@ impl 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) => {