mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-31 11:02:08 +01:00
Add ServiceConfig::default_service
(#2743)
* Add `ServiceConfig::default_service` based on https://github.com/actix/actix-web/pull/2338 * update changelog
This commit is contained in:
parent
9aab911600
commit
b1c85ba85b
@ -4,13 +4,16 @@
|
|||||||
### Added
|
### Added
|
||||||
- Add `ServiceRequest::extract()` to make it easier to use extractors when writing middlewares. [#2647]
|
- Add `ServiceRequest::extract()` to make it easier to use extractors when writing middlewares. [#2647]
|
||||||
- Add `Route::wrap()` to allow individual routes to use middleware. [#2725]
|
- Add `Route::wrap()` to allow individual routes to use middleware. [#2725]
|
||||||
|
- Add `ServiceConfig::default_service()`. [#2338] [#2743]
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- Clear connection-level data on `HttpRequest` drop. [#2742]
|
- Clear connection-level data on `HttpRequest` drop. [#2742]
|
||||||
|
|
||||||
|
[#2338]: https://github.com/actix/actix-web/pull/2338
|
||||||
[#2647]: https://github.com/actix/actix-web/pull/2647
|
[#2647]: https://github.com/actix/actix-web/pull/2647
|
||||||
[#2725]: https://github.com/actix/actix-web/pull/2725
|
[#2725]: https://github.com/actix/actix-web/pull/2725
|
||||||
[#2742]: https://github.com/actix/actix-web/pull/2742
|
[#2742]: https://github.com/actix/actix-web/pull/2742
|
||||||
|
[#2743]: https://github.com/actix/actix-web/pull/2743
|
||||||
|
|
||||||
|
|
||||||
## 4.0.1 - 2022-02-25
|
## 4.0.1 - 2022-02-25
|
||||||
@ -726,7 +729,7 @@
|
|||||||
### Removed
|
### Removed
|
||||||
- Public modules `middleware::{normalize, err_handlers}`. All necessary middleware types are now
|
- Public modules `middleware::{normalize, err_handlers}`. All necessary middleware types are now
|
||||||
exposed directly by the `middleware` module.
|
exposed directly by the `middleware` module.
|
||||||
- Remove `actix-threadpool` as dependency. `actix_threadpool::BlockingError` error type can be imported
|
- Remove `actix-threadpool` as dependency. `actix_threadpool::BlockingError` error type can be imported
|
||||||
from `actix_web::error` module. [#1878]
|
from `actix_web::error` module. [#1878]
|
||||||
|
|
||||||
[#1812]: https://github.com/actix/actix-web/pull/1812
|
[#1812]: https://github.com/actix/actix-web/pull/1812
|
||||||
@ -828,7 +831,7 @@
|
|||||||
|
|
||||||
## 3.0.0-beta.4 - 2020-09-09
|
## 3.0.0-beta.4 - 2020-09-09
|
||||||
### Added
|
### Added
|
||||||
- `middleware::NormalizePath` now has configurable behavior for either always having a trailing
|
- `middleware::NormalizePath` now has configurable behavior for either always having a trailing
|
||||||
slash, or as the new addition, always trimming trailing slashes. [#1639]
|
slash, or as the new addition, always trimming trailing slashes. [#1639]
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -185,10 +185,17 @@ where
|
|||||||
F: FnOnce(&mut ServiceConfig),
|
F: FnOnce(&mut ServiceConfig),
|
||||||
{
|
{
|
||||||
let mut cfg = ServiceConfig::new();
|
let mut cfg = ServiceConfig::new();
|
||||||
|
|
||||||
f(&mut cfg);
|
f(&mut cfg);
|
||||||
|
|
||||||
self.services.extend(cfg.services);
|
self.services.extend(cfg.services);
|
||||||
self.external.extend(cfg.external);
|
self.external.extend(cfg.external);
|
||||||
self.extensions.extend(cfg.app_data);
|
self.extensions.extend(cfg.app_data);
|
||||||
|
|
||||||
|
if let Some(default) = cfg.default {
|
||||||
|
self.default = Some(default);
|
||||||
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,7 +274,6 @@ where
|
|||||||
{
|
{
|
||||||
let svc = svc
|
let svc = svc
|
||||||
.into_factory()
|
.into_factory()
|
||||||
.map(|res| res.map_into_boxed_body())
|
|
||||||
.map_init_err(|e| log::error!("Can not construct default service: {:?}", e));
|
.map_init_err(|e| log::error!("Can not construct default service: {:?}", e));
|
||||||
|
|
||||||
self.default = Some(Rc::new(boxed::factory(svc)));
|
self.default = Some(Rc::new(boxed::factory(svc)));
|
||||||
|
@ -1,33 +1,32 @@
|
|||||||
use std::net::SocketAddr;
|
use std::{net::SocketAddr, rc::Rc};
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use actix_http::Extensions;
|
use actix_service::{boxed, IntoServiceFactory, ServiceFactory, ServiceFactoryExt as _};
|
||||||
use actix_router::ResourceDef;
|
|
||||||
use actix_service::{boxed, IntoServiceFactory, ServiceFactory};
|
|
||||||
|
|
||||||
use crate::data::Data;
|
use crate::{
|
||||||
use crate::error::Error;
|
data::Data,
|
||||||
use crate::guard::Guard;
|
dev::{Extensions, ResourceDef},
|
||||||
use crate::resource::Resource;
|
error::Error,
|
||||||
use crate::rmap::ResourceMap;
|
guard::Guard,
|
||||||
use crate::route::Route;
|
resource::Resource,
|
||||||
use crate::service::{
|
rmap::ResourceMap,
|
||||||
AppServiceFactory, HttpServiceFactory, ServiceFactoryWrapper, ServiceRequest,
|
route::Route,
|
||||||
ServiceResponse,
|
service::{
|
||||||
|
AppServiceFactory, BoxedHttpServiceFactory, HttpServiceFactory, ServiceFactoryWrapper,
|
||||||
|
ServiceRequest, ServiceResponse,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
type Guards = Vec<Box<dyn Guard>>;
|
type Guards = Vec<Box<dyn Guard>>;
|
||||||
type HttpNewService = boxed::BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
|
||||||
|
|
||||||
/// Application configuration
|
/// Application configuration
|
||||||
pub struct AppService {
|
pub struct AppService {
|
||||||
config: AppConfig,
|
config: AppConfig,
|
||||||
root: bool,
|
root: bool,
|
||||||
default: Rc<HttpNewService>,
|
default: Rc<BoxedHttpServiceFactory>,
|
||||||
#[allow(clippy::type_complexity)]
|
#[allow(clippy::type_complexity)]
|
||||||
services: Vec<(
|
services: Vec<(
|
||||||
ResourceDef,
|
ResourceDef,
|
||||||
HttpNewService,
|
BoxedHttpServiceFactory,
|
||||||
Option<Guards>,
|
Option<Guards>,
|
||||||
Option<Rc<ResourceMap>>,
|
Option<Rc<ResourceMap>>,
|
||||||
)>,
|
)>,
|
||||||
@ -35,7 +34,7 @@ pub struct AppService {
|
|||||||
|
|
||||||
impl AppService {
|
impl AppService {
|
||||||
/// Crate server settings instance.
|
/// Crate server settings instance.
|
||||||
pub(crate) fn new(config: AppConfig, default: Rc<HttpNewService>) -> Self {
|
pub(crate) fn new(config: AppConfig, default: Rc<BoxedHttpServiceFactory>) -> Self {
|
||||||
AppService {
|
AppService {
|
||||||
config,
|
config,
|
||||||
default,
|
default,
|
||||||
@ -56,7 +55,7 @@ impl AppService {
|
|||||||
AppConfig,
|
AppConfig,
|
||||||
Vec<(
|
Vec<(
|
||||||
ResourceDef,
|
ResourceDef,
|
||||||
HttpNewService,
|
BoxedHttpServiceFactory,
|
||||||
Option<Guards>,
|
Option<Guards>,
|
||||||
Option<Rc<ResourceMap>>,
|
Option<Rc<ResourceMap>>,
|
||||||
)>,
|
)>,
|
||||||
@ -81,7 +80,7 @@ impl AppService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns default handler factory.
|
/// Returns default handler factory.
|
||||||
pub fn default_service(&self) -> Rc<HttpNewService> {
|
pub fn default_service(&self) -> Rc<BoxedHttpServiceFactory> {
|
||||||
self.default.clone()
|
self.default.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,6 +186,7 @@ pub struct ServiceConfig {
|
|||||||
pub(crate) services: Vec<Box<dyn AppServiceFactory>>,
|
pub(crate) services: Vec<Box<dyn AppServiceFactory>>,
|
||||||
pub(crate) external: Vec<ResourceDef>,
|
pub(crate) external: Vec<ResourceDef>,
|
||||||
pub(crate) app_data: Extensions,
|
pub(crate) app_data: Extensions,
|
||||||
|
pub(crate) default: Option<Rc<BoxedHttpServiceFactory>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServiceConfig {
|
impl ServiceConfig {
|
||||||
@ -195,6 +195,7 @@ impl ServiceConfig {
|
|||||||
services: Vec::new(),
|
services: Vec::new(),
|
||||||
external: Vec::new(),
|
external: Vec::new(),
|
||||||
app_data: Extensions::new(),
|
app_data: Extensions::new(),
|
||||||
|
default: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,6 +216,29 @@ impl ServiceConfig {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Default service to be used if no matching resource could be found.
|
||||||
|
///
|
||||||
|
/// Counterpart to [`App::default_service()`](crate::App::default_service).
|
||||||
|
pub fn default_service<F, U>(&mut self, f: F) -> &mut Self
|
||||||
|
where
|
||||||
|
F: IntoServiceFactory<U, ServiceRequest>,
|
||||||
|
U: ServiceFactory<
|
||||||
|
ServiceRequest,
|
||||||
|
Config = (),
|
||||||
|
Response = ServiceResponse,
|
||||||
|
Error = Error,
|
||||||
|
> + 'static,
|
||||||
|
U::InitError: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
let svc = f
|
||||||
|
.into_factory()
|
||||||
|
.map_init_err(|err| log::error!("Can not construct default service: {:?}", err));
|
||||||
|
|
||||||
|
self.default = Some(Rc::new(boxed::factory(svc)));
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Run external configuration as part of the application building process
|
/// Run external configuration as part of the application building process
|
||||||
///
|
///
|
||||||
/// Counterpart to [`App::configure()`](crate::App::configure) that allows for easy nesting.
|
/// Counterpart to [`App::configure()`](crate::App::configure) that allows for easy nesting.
|
||||||
@ -322,6 +346,38 @@ mod tests {
|
|||||||
assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345"));
|
assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn registers_default_service() {
|
||||||
|
let srv = init_service(
|
||||||
|
App::new()
|
||||||
|
.configure(|cfg| {
|
||||||
|
cfg.default_service(
|
||||||
|
web::get().to(|| HttpResponse::NotFound().body("four oh four")),
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.service(web::scope("/scoped").configure(|cfg| {
|
||||||
|
cfg.default_service(
|
||||||
|
web::get().to(|| HttpResponse::NotFound().body("scoped four oh four")),
|
||||||
|
);
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// app registers default service
|
||||||
|
let req = TestRequest::with_uri("/path/i/did/not-configure").to_request();
|
||||||
|
let resp = call_service(&srv, req).await;
|
||||||
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
|
let body = read_body(resp).await;
|
||||||
|
assert_eq!(body, Bytes::from_static(b"four oh four"));
|
||||||
|
|
||||||
|
// scope registers default service
|
||||||
|
let req = TestRequest::with_uri("/scoped/path/i/did/not-configure").to_request();
|
||||||
|
let resp = call_service(&srv, req).await;
|
||||||
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
|
let body = read_body(resp).await;
|
||||||
|
assert_eq!(body, Bytes::from_static(b"scoped four oh four"));
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_service() {
|
async fn test_service() {
|
||||||
let srv = init_service(App::new().configure(|cfg| {
|
let srv = init_service(App::new().configure(|cfg| {
|
||||||
|
@ -198,6 +198,10 @@ where
|
|||||||
.get_or_insert_with(Extensions::new)
|
.get_or_insert_with(Extensions::new)
|
||||||
.extend(cfg.app_data);
|
.extend(cfg.app_data);
|
||||||
|
|
||||||
|
if let Some(default) = cfg.default {
|
||||||
|
self.default = Some(default);
|
||||||
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user