mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
enable scope middleware with generic res body. (#2492)
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
parent
6460e67f84
commit
069cf2da07
@ -13,6 +13,8 @@
|
|||||||
* Rename `Accept::{mime_preference => preference}`. [#2480]
|
* Rename `Accept::{mime_preference => preference}`. [#2480]
|
||||||
* Un-deprecate `App::data_factory`. [#2484]
|
* Un-deprecate `App::data_factory`. [#2484]
|
||||||
* `HttpRequest::url_for` no longer constructs URLs with query or fragment components. [#2430]
|
* `HttpRequest::url_for` no longer constructs URLs with query or fragment components. [#2430]
|
||||||
|
* Remove `B` (body) type parameter on `App`. [#2493]
|
||||||
|
* Add `B` (body) type parameter on `Scope`. [#2492]
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
* Accept wildcard `*` items in `AcceptLanguage`. [#2480]
|
* Accept wildcard `*` items in `AcceptLanguage`. [#2480]
|
||||||
@ -25,6 +27,8 @@
|
|||||||
[#2482]: https://github.com/actix/actix-web/pull/2482
|
[#2482]: https://github.com/actix/actix-web/pull/2482
|
||||||
[#2484]: https://github.com/actix/actix-web/pull/2484
|
[#2484]: https://github.com/actix/actix-web/pull/2484
|
||||||
[#2485]: https://github.com/actix/actix-web/pull/2485
|
[#2485]: https://github.com/actix/actix-web/pull/2485
|
||||||
|
[#2492]: https://github.com/actix/actix-web/pull/2492
|
||||||
|
[#2493]: https://github.com/actix/actix-web/pull/2493
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.13 - 2021-11-30
|
## 4.0.0-beta.13 - 2021-11-30
|
||||||
|
@ -154,7 +154,7 @@ mod tests {
|
|||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("app")
|
web::scope("app")
|
||||||
.wrap(Compat::new(logger))
|
.wrap(logger)
|
||||||
.wrap(Compat::new(compress))
|
.wrap(Compat::new(compress))
|
||||||
.service(web::resource("/test").route(web::get().to(HttpResponse::Ok))),
|
.service(web::resource("/test").route(web::get().to(HttpResponse::Ok))),
|
||||||
),
|
),
|
||||||
|
49
src/scope.rs
49
src/scope.rs
@ -1,6 +1,6 @@
|
|||||||
use std::{cell::RefCell, fmt, future::Future, mem, rc::Rc};
|
use std::{cell::RefCell, fmt, future::Future, marker::PhantomData, mem, rc::Rc};
|
||||||
|
|
||||||
use actix_http::Extensions;
|
use actix_http::{body::BoxBody, Extensions};
|
||||||
use actix_router::{ResourceDef, Router};
|
use actix_router::{ResourceDef, Router};
|
||||||
use actix_service::{
|
use actix_service::{
|
||||||
apply, apply_fn_factory, boxed, IntoServiceFactory, Service, ServiceFactory,
|
apply, apply_fn_factory, boxed, IntoServiceFactory, Service, ServiceFactory,
|
||||||
@ -52,7 +52,7 @@ type Guards = Vec<Box<dyn Guard>>;
|
|||||||
/// * /{project_id}/path1 - responds to all http method
|
/// * /{project_id}/path1 - responds to all http method
|
||||||
/// * /{project_id}/path2 - `GET` requests
|
/// * /{project_id}/path2 - `GET` requests
|
||||||
/// * /{project_id}/path3 - `HEAD` requests
|
/// * /{project_id}/path3 - `HEAD` requests
|
||||||
pub struct Scope<T = ScopeEndpoint> {
|
pub struct Scope<T = ScopeEndpoint, B = BoxBody> {
|
||||||
endpoint: T,
|
endpoint: T,
|
||||||
rdef: String,
|
rdef: String,
|
||||||
app_data: Option<Extensions>,
|
app_data: Option<Extensions>,
|
||||||
@ -61,6 +61,7 @@ pub struct Scope<T = ScopeEndpoint> {
|
|||||||
default: Option<Rc<BoxedHttpServiceFactory>>,
|
default: Option<Rc<BoxedHttpServiceFactory>>,
|
||||||
external: Vec<ResourceDef>,
|
external: Vec<ResourceDef>,
|
||||||
factory_ref: Rc<RefCell<Option<ScopeFactory>>>,
|
factory_ref: Rc<RefCell<Option<ScopeFactory>>>,
|
||||||
|
_phantom: PhantomData<B>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Scope {
|
impl Scope {
|
||||||
@ -77,19 +78,21 @@ impl Scope {
|
|||||||
default: None,
|
default: None,
|
||||||
external: Vec::new(),
|
external: Vec::new(),
|
||||||
factory_ref,
|
factory_ref,
|
||||||
|
_phantom: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Scope<T>
|
impl<T, B> Scope<T, B>
|
||||||
where
|
where
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
ServiceRequest,
|
ServiceRequest,
|
||||||
Config = (),
|
Config = (),
|
||||||
Response = ServiceResponse,
|
Response = ServiceResponse<B>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
|
B: 'static,
|
||||||
{
|
{
|
||||||
/// Add match guard to a scope.
|
/// Add match guard to a scope.
|
||||||
///
|
///
|
||||||
@ -295,32 +298,29 @@ where
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Registers middleware, in the form of a middleware component (type),
|
/// Registers middleware, in the form of a middleware component (type), that runs during inbound
|
||||||
/// that runs during inbound processing in the request
|
/// processing in the request life-cycle (request -> response), modifying request as necessary,
|
||||||
/// life-cycle (request -> response), modifying request as
|
/// across all requests managed by the *Scope*.
|
||||||
/// necessary, across all requests managed by the *Scope*. Scope-level
|
|
||||||
/// middleware is more limited in what it can modify, relative to Route or
|
|
||||||
/// Application level middleware, in that Scope-level middleware can not modify
|
|
||||||
/// ServiceResponse.
|
|
||||||
///
|
///
|
||||||
/// Use middleware when you need to read or modify *every* request in some way.
|
/// Use middleware when you need to read or modify *every* request in some way.
|
||||||
pub fn wrap<M>(
|
pub fn wrap<M, B1>(
|
||||||
self,
|
self,
|
||||||
mw: M,
|
mw: M,
|
||||||
) -> Scope<
|
) -> Scope<
|
||||||
impl ServiceFactory<
|
impl ServiceFactory<
|
||||||
ServiceRequest,
|
ServiceRequest,
|
||||||
Config = (),
|
Config = (),
|
||||||
Response = ServiceResponse,
|
Response = ServiceResponse<B1>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
|
B1,
|
||||||
>
|
>
|
||||||
where
|
where
|
||||||
M: Transform<
|
M: Transform<
|
||||||
T::Service,
|
T::Service,
|
||||||
ServiceRequest,
|
ServiceRequest,
|
||||||
Response = ServiceResponse,
|
Response = ServiceResponse<B1>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
@ -334,16 +334,15 @@ where
|
|||||||
default: self.default,
|
default: self.default,
|
||||||
external: self.external,
|
external: self.external,
|
||||||
factory_ref: self.factory_ref,
|
factory_ref: self.factory_ref,
|
||||||
|
_phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Registers middleware, in the form of a closure, that runs during inbound
|
/// Registers middleware, in the form of a closure, that runs during inbound processing in the
|
||||||
/// processing in the request life-cycle (request -> response), modifying
|
/// request life-cycle (request -> response), modifying request as necessary, across all
|
||||||
/// request as necessary, across all requests managed by the *Scope*.
|
/// requests managed by the *Scope*.
|
||||||
/// Scope-level middleware is more limited in what it can modify, relative
|
|
||||||
/// to Route or Application level middleware, in that Scope-level middleware
|
|
||||||
/// can not modify ServiceResponse.
|
|
||||||
///
|
///
|
||||||
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// use actix_service::Service;
|
/// use actix_service::Service;
|
||||||
/// use actix_web::{web, App};
|
/// use actix_web::{web, App};
|
||||||
@ -369,21 +368,22 @@ where
|
|||||||
/// .route("/index.html", web::get().to(index)));
|
/// .route("/index.html", web::get().to(index)));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn wrap_fn<F, R>(
|
pub fn wrap_fn<F, R, B1>(
|
||||||
self,
|
self,
|
||||||
mw: F,
|
mw: F,
|
||||||
) -> Scope<
|
) -> Scope<
|
||||||
impl ServiceFactory<
|
impl ServiceFactory<
|
||||||
ServiceRequest,
|
ServiceRequest,
|
||||||
Config = (),
|
Config = (),
|
||||||
Response = ServiceResponse,
|
Response = ServiceResponse<B1>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
|
B1,
|
||||||
>
|
>
|
||||||
where
|
where
|
||||||
F: Fn(ServiceRequest, &T::Service) -> R + Clone,
|
F: Fn(ServiceRequest, &T::Service) -> R + Clone,
|
||||||
R: Future<Output = Result<ServiceResponse, Error>>,
|
R: Future<Output = Result<ServiceResponse<B1>, Error>>,
|
||||||
{
|
{
|
||||||
Scope {
|
Scope {
|
||||||
endpoint: apply_fn_factory(self.endpoint, mw),
|
endpoint: apply_fn_factory(self.endpoint, mw),
|
||||||
@ -394,6 +394,7 @@ where
|
|||||||
default: self.default,
|
default: self.default,
|
||||||
external: self.external,
|
external: self.external,
|
||||||
factory_ref: self.factory_ref,
|
factory_ref: self.factory_ref,
|
||||||
|
_phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user