Skip to content

Commit

Permalink
Introduce GrpcWebLayer implementing tower::Layer. Fix hyperium#1117
Browse files Browse the repository at this point in the history
Signed-off-by: slinkydeveloper <[email protected]>
  • Loading branch information
slinkydeveloper committed Oct 24, 2022
1 parent 60c0c54 commit fb2fe58
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
15 changes: 0 additions & 15 deletions tonic-web/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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;
Expand Down Expand Up @@ -165,17 +164,3 @@ impl Default for Config {
Config::new()
}
}

impl<S> Layer<S> for Config
where
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
S: Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<BoxError> + Send,
{
type Service = GrpcWeb<S>;

fn layer(&self, inner: S) -> Self::Service {
self.enable(inner)
}
}
31 changes: 31 additions & 0 deletions tonic-web/src/layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::{BoxBody, BoxError, Config, GrpcWeb};

use tower_layer::Layer;
use tower_service::Service;

/// Layer implementing the grpc-web protocol.
#[derive(Debug)]
pub struct GrpcWebLayer {
_priv: (),
}

impl GrpcWebLayer {
/// Create a new grpc-web layer.
pub fn new() -> GrpcWebLayer {
Self { _priv: () }
}
}

impl<S> Layer<S> for GrpcWebLayer
where
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
S: Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<BoxError> + Send,
{
type Service = GrpcWeb<S>;

fn layer(&self, inner: S) -> Self::Service {
Config::default().enable(inner)
}
}
3 changes: 3 additions & 0 deletions tonic-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub use config::Config;
mod call;
mod config;
mod cors;
mod layer;
mod service;

use crate::service::GrpcWeb;
Expand All @@ -101,6 +102,8 @@ use tonic::body::BoxBody;
use tonic::transport::NamedService;
use tower_service::Service;

pub use layer::GrpcWebLayer;

/// enable a tonic service to handle grpc-web requests with the default configuration.
///
/// Shortcut for `tonic_web::config().enable(service)`
Expand Down
9 changes: 9 additions & 0 deletions tonic-web/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ mod tests {
mod grpc_web {
use super::*;
use http::HeaderValue;
use tower_layer::Layer;

fn request() -> Request<Body> {
Request::builder()
Expand All @@ -284,6 +285,14 @@ mod tests {
assert_eq!(res.status(), StatusCode::OK);
}

#[tokio::test]
async fn web_layer() {
let mut svc = crate::GrpcWebLayer::new().layer(Svc);
let res = svc.call(request()).await.unwrap();

assert_eq!(res.status(), StatusCode::OK);
}

#[tokio::test]
async fn without_origin() {
let mut svc = crate::enable(Svc);
Expand Down
7 changes: 4 additions & 3 deletions tonic-web/tests/integration/tests/grpc_web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ async fn spawn(allowed_origin: &str) -> String {
let url = format!("http://{}", listener.local_addr().unwrap());
let listener_stream = TcpListenerStream::new(listener);

let tonic_web_config = tonic_web::config().allow_origins(vec![allowed_origin]);
let svc = tonic_web::config()
.allow_origins(vec![allowed_origin])
.enable(TestServer::new(Svc));

let _ = tokio::spawn(async move {
Server::builder()
.accept_http1(true)
.layer(tonic_web_config)
.add_service(TestServer::new(Svc))
.add_service(svc)
.serve_with_incoming(listener_stream)
.await
.unwrap()
Expand Down

0 comments on commit fb2fe58

Please sign in to comment.