Skip to content

Commit

Permalink
feat(web): Implement tower::Layer for tonic_web::Config (#1119)
Browse files Browse the repository at this point in the history
Signed-off-by: slinkydeveloper <[email protected]>
  • Loading branch information
slinkydeveloper authored Oct 28, 2022
1 parent b409ddd commit 40536dc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions tonic-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ hyper = "0.14"
pin-project = "1"
tonic = {version = "0.8", path = "../tonic", default-features = false, features = ["transport"]}
tower-service = "0.3"
tower-layer = "0.3"
tracing = "0.1"

[dev-dependencies]
Expand Down
4 changes: 1 addition & 3 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 tonic::transport::NamedService;
use tower_service::Service;

use crate::service::GrpcWeb;
Expand Down Expand Up @@ -152,11 +151,10 @@ impl Config {
pub fn enable<S>(&self, service: S) -> GrpcWeb<S>
where
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
S: NamedService + Clone + Send + 'static,
S: Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<BoxError> + Send,
{
tracing::trace!("enabled for {}", S::NAME);
GrpcWeb::new(service, self.clone())
}
}
Expand Down
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, Clone)]
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)
}
}
7 changes: 4 additions & 3 deletions tonic-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,18 @@
#![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;
use tonic::transport::NamedService;
use tower_service::Service;

/// enable a tonic service to handle grpc-web requests with the default configuration.
Expand All @@ -107,7 +108,7 @@ use tower_service::Service;
pub fn enable<S>(service: S) -> GrpcWeb<S>
where
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
S: NamedService + Clone + Send + 'static,
S: Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<BoxError> + Send,
{
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

0 comments on commit 40536dc

Please sign in to comment.