Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tower::Layer for tonic_web::Config #1119

Merged
merged 4 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]
pub struct GrpcWebLayer {
_priv: (),
LucioFranco marked this conversation as resolved.
Show resolved Hide resolved
}

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