feat(web): Removed Cors impl and replaced with tower-http's CorsLayer (#1123)
Fix #1122, see the issue for more details. Signed-off-by: slinkydeveloper <[email protected]>
This commit is contained in:
+37
-32
@@ -34,7 +34,8 @@
|
||||
//!
|
||||
//! ```
|
||||
//! This will apply a default configuration that works well with grpc-web clients out of the box.
|
||||
//! See the [`Config`] documentation for details.
|
||||
//!
|
||||
//! You can customize the CORS configuration composing the [`GrpcWebLayer`] with the cors layer of your choice.
|
||||
//!
|
||||
//! Alternatively, if you have a tls enabled server, you could skip setting `accept_http1` to `true`.
|
||||
//! This works because the browser will handle `ALPN`.
|
||||
@@ -77,7 +78,6 @@
|
||||
//! [grpc-web]: https://github.com/grpc/grpc-web
|
||||
//! [tower]: https://github.com/tower-rs/tower
|
||||
//! [`enable`]: crate::enable()
|
||||
//! [`Config`]: crate::Config
|
||||
#![warn(
|
||||
missing_debug_implementations,
|
||||
missing_docs,
|
||||
@@ -87,50 +87,55 @@
|
||||
#![doc(html_root_url = "https://docs.rs/tonic-web/0.4.0")]
|
||||
#![doc(issue_tracker_base_url = "https://github.com/hyperium/tonic/issues/")]
|
||||
|
||||
pub use config::Config;
|
||||
pub use layer::GrpcWebLayer;
|
||||
pub use service::GrpcWeb;
|
||||
pub use service::{GrpcWebService, ResponseFuture};
|
||||
|
||||
mod call;
|
||||
mod config;
|
||||
mod cors;
|
||||
mod layer;
|
||||
mod service;
|
||||
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use http::header::HeaderName;
|
||||
use std::time::Duration;
|
||||
use tonic::body::BoxBody;
|
||||
use tower_http::cors::{AllowOrigin, Cors, CorsLayer};
|
||||
use tower_layer::Layer;
|
||||
use tower_service::Service;
|
||||
|
||||
/// enable a tonic service to handle grpc-web requests with the default configuration.
|
||||
const DEFAULT_MAX_AGE: Duration = Duration::from_secs(24 * 60 * 60);
|
||||
const DEFAULT_EXPOSED_HEADERS: [&str; 3] =
|
||||
["grpc-status", "grpc-message", "grpc-status-details-bin"];
|
||||
const DEFAULT_ALLOW_HEADERS: [&str; 4] =
|
||||
["x-grpc-web", "content-type", "x-user-agent", "grpc-timeout"];
|
||||
|
||||
type BoxError = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
/// Enable a tonic service to handle grpc-web requests with the default configuration.
|
||||
///
|
||||
/// Shortcut for `tonic_web::config().enable(service)`
|
||||
pub fn enable<S>(service: S) -> GrpcWeb<S>
|
||||
/// 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>>
|
||||
where
|
||||
S: Service<http::Request<hyper::Body>, Response = http::Response<BoxBody>>,
|
||||
S: Clone + Send + 'static,
|
||||
S::Future: Send + 'static,
|
||||
S::Error: Into<BoxError> + Send,
|
||||
{
|
||||
config().enable(service)
|
||||
CorsLayer::new()
|
||||
.allow_origin(AllowOrigin::mirror_request())
|
||||
.allow_credentials(true)
|
||||
.max_age(DEFAULT_MAX_AGE)
|
||||
.expose_headers(
|
||||
DEFAULT_EXPOSED_HEADERS
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(HeaderName::from_static)
|
||||
.collect::<Vec<HeaderName>>(),
|
||||
)
|
||||
.allow_headers(
|
||||
DEFAULT_ALLOW_HEADERS
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(HeaderName::from_static)
|
||||
.collect::<Vec<HeaderName>>(),
|
||||
)
|
||||
.layer(GrpcWebService::new(service))
|
||||
}
|
||||
|
||||
/// returns a default [`Config`] instance for configuring services.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```
|
||||
/// let config = tonic_web::config()
|
||||
/// .allow_origins(vec!["http://foo.com"])
|
||||
/// .allow_credentials(false)
|
||||
/// .expose_headers(vec!["x-request-id"]);
|
||||
///
|
||||
/// // let greeter = config.enable(Greeter);
|
||||
/// // let route_guide = config.enable(RouteGuide);
|
||||
/// ```
|
||||
pub fn config() -> Config {
|
||||
Config::default()
|
||||
}
|
||||
|
||||
type BoxError = Box<dyn std::error::Error + Send + Sync>;
|
||||
type BoxFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;
|
||||
|
||||
Reference in New Issue
Block a user