Skip to content

Commit 40536dc

Browse files
authoredOct 28, 2022
feat(web): Implement tower::Layer for tonic_web::Config (#1119)
Signed-off-by: slinkydeveloper <francescoguard@gmail.com>
1 parent b409ddd commit 40536dc

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed
 

‎tonic-web/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ hyper = "0.14"
2424
pin-project = "1"
2525
tonic = {version = "0.8", path = "../tonic", default-features = false, features = ["transport"]}
2626
tower-service = "0.3"
27+
tower-layer = "0.3"
2728
tracing = "0.1"
2829

2930
[dev-dependencies]

‎tonic-web/src/config.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::time::Duration;
44

55
use http::{header::HeaderName, HeaderValue};
66
use tonic::body::BoxBody;
7-
use tonic::transport::NamedService;
87
use tower_service::Service;
98

109
use crate::service::GrpcWeb;
@@ -152,11 +151,10 @@ impl Config {
152151
pub fn enable<S>(&self, service: S) -> GrpcWeb<S>
153152
where
154153
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
155-
S: NamedService + Clone + Send + 'static,
154+
S: Clone + Send + 'static,
156155
S::Future: Send + 'static,
157156
S::Error: Into<BoxError> + Send,
158157
{
159-
tracing::trace!("enabled for {}", S::NAME);
160158
GrpcWeb::new(service, self.clone())
161159
}
162160
}

‎tonic-web/src/layer.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use super::{BoxBody, BoxError, Config, GrpcWeb};
2+
3+
use tower_layer::Layer;
4+
use tower_service::Service;
5+
6+
/// Layer implementing the grpc-web protocol.
7+
#[derive(Debug, Clone)]
8+
pub struct GrpcWebLayer {
9+
_priv: (),
10+
}
11+
12+
impl GrpcWebLayer {
13+
/// Create a new grpc-web layer.
14+
pub fn new() -> GrpcWebLayer {
15+
Self { _priv: () }
16+
}
17+
}
18+
19+
impl<S> Layer<S> for GrpcWebLayer
20+
where
21+
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
22+
S: Clone + Send + 'static,
23+
S::Future: Send + 'static,
24+
S::Error: Into<BoxError> + Send,
25+
{
26+
type Service = GrpcWeb<S>;
27+
28+
fn layer(&self, inner: S) -> Self::Service {
29+
Config::default().enable(inner)
30+
}
31+
}

‎tonic-web/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,18 @@
8888
#![doc(issue_tracker_base_url = "https://github.com/hyperium/tonic/issues/")]
8989

9090
pub use config::Config;
91+
pub use layer::GrpcWebLayer;
92+
pub use service::GrpcWeb;
9193

9294
mod call;
9395
mod config;
9496
mod cors;
97+
mod layer;
9598
mod service;
9699

97-
use crate::service::GrpcWeb;
98100
use std::future::Future;
99101
use std::pin::Pin;
100102
use tonic::body::BoxBody;
101-
use tonic::transport::NamedService;
102103
use tower_service::Service;
103104

104105
/// enable a tonic service to handle grpc-web requests with the default configuration.
@@ -107,7 +108,7 @@ use tower_service::Service;
107108
pub fn enable<S>(service: S) -> GrpcWeb<S>
108109
where
109110
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
110-
S: NamedService + Clone + Send + 'static,
111+
S: Clone + Send + 'static,
111112
S::Future: Send + 'static,
112113
S::Error: Into<BoxError> + Send,
113114
{

‎tonic-web/src/service.rs

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{BoxError, BoxFuture, Config};
1515

1616
const GRPC: &str = "application/grpc";
1717

18+
/// Service implementing the grpc-web protocol.
1819
#[derive(Debug, Clone)]
1920
pub struct GrpcWeb<S> {
2021
inner: S,
@@ -266,6 +267,7 @@ mod tests {
266267
mod grpc_web {
267268
use super::*;
268269
use http::HeaderValue;
270+
use tower_layer::Layer;
269271

270272
fn request() -> Request<Body> {
271273
Request::builder()
@@ -284,6 +286,14 @@ mod tests {
284286
assert_eq!(res.status(), StatusCode::OK);
285287
}
286288

289+
#[tokio::test]
290+
async fn web_layer() {
291+
let mut svc = crate::GrpcWebLayer::new().layer(Svc);
292+
let res = svc.call(request()).await.unwrap();
293+
294+
assert_eq!(res.status(), StatusCode::OK);
295+
}
296+
287297
#[tokio::test]
288298
async fn without_origin() {
289299
let mut svc = crate::enable(Svc);

0 commit comments

Comments
 (0)
Please sign in to comment.