diff --git a/tonic-web/src/config.rs b/tonic-web/src/config.rs index 5c965a18a..9e3906cb3 100644 --- a/tonic-web/src/config.rs +++ b/tonic-web/src/config.rs @@ -4,6 +4,7 @@ use std::time::Duration; use http::{header::HeaderName, HeaderValue}; use tonic::body::BoxBody; +use tower_layer::Layer; use tower_service::Service; use crate::service::GrpcWeb; @@ -164,3 +165,17 @@ impl Default for Config { Config::new() } } + +impl Layer for Config +where + S: Service, Response = http::Response>, + S: Clone + Send + 'static, + S::Future: Send + 'static, + S::Error: Into + Send, +{ + type Service = GrpcWeb; + + fn layer(&self, inner: S) -> Self::Service { + self.enable(inner) + } +} diff --git a/tonic-web/tests/integration/tests/grpc_web.rs b/tonic-web/tests/integration/tests/grpc_web.rs index 61b686652..93b57bf01 100644 --- a/tonic-web/tests/integration/tests/grpc_web.rs +++ b/tonic-web/tests/integration/tests/grpc_web.rs @@ -74,14 +74,13 @@ async fn spawn(allowed_origin: &str) -> String { let url = format!("http://{}", listener.local_addr().unwrap()); let listener_stream = TcpListenerStream::new(listener); - let svc = tonic_web::config() - .allow_origins(vec![allowed_origin]) - .enable(TestServer::new(Svc)); + let tonic_web_config = tonic_web::config().allow_origins(vec![allowed_origin]); let _ = tokio::spawn(async move { Server::builder() .accept_http1(true) - .add_service(svc) + .layer(tonic_web_config) + .add_service(TestServer::new(Svc)) .serve_with_incoming(listener_stream) .await .unwrap()