fix(web): Fix enable and update docs (#1326)
* fix(web): Fix `enable` and update docs * fix example, update deny * fix namedservice path
This commit is contained in:
@@ -18,6 +18,7 @@ skip-tree = [
|
|||||||
{ name = "rustls-pemfile" },
|
{ name = "rustls-pemfile" },
|
||||||
{ name = "windows-sys" },
|
{ name = "windows-sys" },
|
||||||
{ name = "hermit-abi" },
|
{ name = "hermit-abi" },
|
||||||
|
{ name = "syn" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[licenses]
|
[licenses]
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ use tonic::{transport::Server, Request, Response, Status};
|
|||||||
|
|
||||||
use hello_world::greeter_server::{Greeter, GreeterServer};
|
use hello_world::greeter_server::{Greeter, GreeterServer};
|
||||||
use hello_world::{HelloReply, HelloRequest};
|
use hello_world::{HelloReply, HelloRequest};
|
||||||
use tonic_web::GrpcWebLayer;
|
|
||||||
use tower_http::cors::CorsLayer;
|
|
||||||
|
|
||||||
pub mod hello_world {
|
pub mod hello_world {
|
||||||
tonic::include_proto!("helloworld");
|
tonic::include_proto!("helloworld");
|
||||||
@@ -41,13 +39,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
Server::builder()
|
Server::builder()
|
||||||
// GrpcWeb is over http1 so we must enable it.
|
// GrpcWeb is over http1 so we must enable it.
|
||||||
.accept_http1(true)
|
.accept_http1(true)
|
||||||
// Use the Cors layer from `tower-http`.
|
.add_service(tonic_web::enable(greeter))
|
||||||
.layer(CorsLayer::new())
|
|
||||||
// Apply the tonic-web layer to convert
|
|
||||||
// http1 requests into something that
|
|
||||||
// the core tonic code can understand.
|
|
||||||
.layer(GrpcWebLayer::new())
|
|
||||||
.add_service(greeter)
|
|
||||||
.serve(addr)
|
.serve(addr)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
+61
-14
@@ -5,13 +5,6 @@
|
|||||||
//! with a [tower] service that performs the translation between protocols and handles `cors`
|
//! with a [tower] service that performs the translation between protocols and handles `cors`
|
||||||
//! requests.
|
//! requests.
|
||||||
//!
|
//!
|
||||||
//! ## Getting Started
|
|
||||||
//!
|
|
||||||
//! ```toml
|
|
||||||
//! [dependencies]
|
|
||||||
//! tonic_web = "0.1"
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! ## Enabling tonic services
|
//! ## Enabling tonic services
|
||||||
//!
|
//!
|
||||||
//! The easiest way to get started, is to call the [`enable`] function with your tonic service
|
//! The easiest way to get started, is to call the [`enable`] function with your tonic service
|
||||||
@@ -31,12 +24,29 @@
|
|||||||
//!
|
//!
|
||||||
//! Ok(())
|
//! Ok(())
|
||||||
//! }
|
//! }
|
||||||
//!
|
|
||||||
//! ```
|
//! ```
|
||||||
//! This will apply a default configuration that works well with grpc-web clients out of the box.
|
//! This will apply a default configuration that works well with grpc-web clients out of the box.
|
||||||
//!
|
//!
|
||||||
//! You can customize the CORS configuration composing the [`GrpcWebLayer`] with the cors layer of your choice.
|
//! You can customize the CORS configuration composing the [`GrpcWebLayer`] with the cors layer of your choice.
|
||||||
//!
|
//!
|
||||||
|
//! ```ignore
|
||||||
|
//! #[tokio::main]
|
||||||
|
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
//! let addr = "[::1]:50051".parse().unwrap();
|
||||||
|
//! let greeter = GreeterServer::new(MyGreeter::default());
|
||||||
|
//!
|
||||||
|
//! Server::builder()
|
||||||
|
//! .accept_http1(true)
|
||||||
|
//! // This will apply the gRPC-Web translation layer
|
||||||
|
//! .layer(GrpcWebLayer::new())
|
||||||
|
//! .add_service(greeter)
|
||||||
|
//! .serve(addr)
|
||||||
|
//! .await?;
|
||||||
|
//!
|
||||||
|
//! Ok(())
|
||||||
|
//! }
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
//! Alternatively, if you have a tls enabled server, you could skip setting `accept_http1` to `true`.
|
//! Alternatively, if you have a tls enabled server, you could skip setting `accept_http1` to `true`.
|
||||||
//! This works because the browser will handle `ALPN`.
|
//! This works because the browser will handle `ALPN`.
|
||||||
//!
|
//!
|
||||||
@@ -96,8 +106,8 @@ mod service;
|
|||||||
|
|
||||||
use http::header::HeaderName;
|
use http::header::HeaderName;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tonic::body::BoxBody;
|
use tonic::{body::BoxBody, server::NamedService};
|
||||||
use tower_http::cors::{AllowOrigin, Cors, CorsLayer};
|
use tower_http::cors::{AllowOrigin, CorsLayer};
|
||||||
use tower_layer::Layer;
|
use tower_layer::Layer;
|
||||||
use tower_service::Service;
|
use tower_service::Service;
|
||||||
|
|
||||||
@@ -112,14 +122,14 @@ type BoxError = Box<dyn std::error::Error + Send + Sync>;
|
|||||||
/// Enable a tonic service to handle grpc-web requests with the default configuration.
|
/// Enable a tonic service to handle grpc-web requests with the default configuration.
|
||||||
///
|
///
|
||||||
/// You can customize the CORS configuration composing the [`GrpcWebLayer`] with the cors layer of your choice.
|
/// You can customize the CORS configuration composing the [`GrpcWebLayer`] with the cors layer of your choice.
|
||||||
pub fn enable<S>(service: S) -> Cors<GrpcWebService<S>>
|
pub fn enable<S>(service: S) -> CorsGrpcWeb<S>
|
||||||
where
|
where
|
||||||
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
|
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
|
||||||
S: Clone + Send + 'static,
|
S: Clone + Send + 'static,
|
||||||
S::Future: Send + 'static,
|
S::Future: Send + 'static,
|
||||||
S::Error: Into<BoxError> + Send,
|
S::Error: Into<BoxError> + Send,
|
||||||
{
|
{
|
||||||
CorsLayer::new()
|
let cors = CorsLayer::new()
|
||||||
.allow_origin(AllowOrigin::mirror_request())
|
.allow_origin(AllowOrigin::mirror_request())
|
||||||
.allow_credentials(true)
|
.allow_credentials(true)
|
||||||
.max_age(DEFAULT_MAX_AGE)
|
.max_age(DEFAULT_MAX_AGE)
|
||||||
@@ -136,8 +146,45 @@ where
|
|||||||
.cloned()
|
.cloned()
|
||||||
.map(HeaderName::from_static)
|
.map(HeaderName::from_static)
|
||||||
.collect::<Vec<HeaderName>>(),
|
.collect::<Vec<HeaderName>>(),
|
||||||
)
|
);
|
||||||
.layer(GrpcWebService::new(service))
|
|
||||||
|
tower_layer::layer_fn(|s| CorsGrpcWeb(cors.layer(s))).layer(GrpcWebService::new(service))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A newtype wrapper around [`GrpcWebLayer`] and [`tower_http::cors::CorsLayer`] to allow
|
||||||
|
/// `tonic_web::enable` to implement the [`NamedService`] trait.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct CorsGrpcWeb<S>(tower_http::cors::Cors<GrpcWebService<S>>);
|
||||||
|
|
||||||
|
impl<S> Service<http::Request<hyper::Body>> for CorsGrpcWeb<S>
|
||||||
|
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 Response = S::Response;
|
||||||
|
type Error = S::Error;
|
||||||
|
type Future =
|
||||||
|
<tower_http::cors::Cors<GrpcWebService<S>> as Service<http::Request<hyper::Body>>>::Future;
|
||||||
|
|
||||||
|
fn poll_ready(
|
||||||
|
&mut self,
|
||||||
|
cx: &mut std::task::Context<'_>,
|
||||||
|
) -> std::task::Poll<Result<(), Self::Error>> {
|
||||||
|
self.0.poll_ready(cx)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, req: http::Request<hyper::Body>) -> Self::Future {
|
||||||
|
self.0.call(req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> NamedService for CorsGrpcWeb<S>
|
||||||
|
where
|
||||||
|
S: NamedService,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = S::NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) mod util {
|
pub(crate) mod util {
|
||||||
|
|||||||
Reference in New Issue
Block a user