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 <francescoguard@gmail.com>
  • Loading branch information
slinkydeveloper committed Oct 24, 2022
1 parent 32e87b3 commit 4b20717
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 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)
}
}
4 changes: 3 additions & 1 deletion tonic-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@
#![doc(issue_tracker_base_url = "https://github.com/hyperium/tonic/issues/")]

pub use config::Config;
pub use layer::GrpcWebLayer;
pub use service::GrpcWeb;

mod call;
mod config;
mod cors;
mod layer;
mod service;

use crate::service::GrpcWeb;
use std::future::Future;
use std::pin::Pin;
use tonic::body::BoxBody;
Expand Down
10 changes: 10 additions & 0 deletions tonic-web/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{BoxError, BoxFuture, Config};

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

/// Service implementing the grpc-web protocol.
#[derive(Debug, Clone)]
pub struct GrpcWeb<S> {
inner: S,
Expand Down Expand Up @@ -266,6 +267,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 +286,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 4b20717

Please sign in to comment.