mirror of
https://github.com/fafhrd91/actix-web
synced 2024-12-02 19:32:24 +01:00
Adding app_data to ServiceConfig (#1758)
Co-authored-by: Rob Ede <robjtede@icloud.com> Co-authored-by: Augusto <augusto@flowciety.de>
This commit is contained in:
parent
20078fe603
commit
7030bf5fe8
@ -4,6 +4,7 @@
|
|||||||
### Added
|
### Added
|
||||||
* Implement `exclude_regex` for Logger middleware. [#1723]
|
* Implement `exclude_regex` for Logger middleware. [#1723]
|
||||||
* Add request-local data extractor `web::ReqData`. [#1748]
|
* Add request-local data extractor `web::ReqData`. [#1748]
|
||||||
|
* Add `app_data` to `ServiceConfig`. [#1757]
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
* Print non-configured `Data<T>` type when attempting extraction. [#1743]
|
* Print non-configured `Data<T>` type when attempting extraction. [#1743]
|
||||||
|
@ -61,6 +61,11 @@ impl Extensions {
|
|||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
self.map.clear();
|
self.map.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extends self with the items from another `Extensions`.
|
||||||
|
pub fn extend(&mut self, other: Extensions) {
|
||||||
|
self.map.extend(other.map);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Extensions {
|
impl fmt::Debug for Extensions {
|
||||||
@ -178,4 +183,34 @@ mod tests {
|
|||||||
assert_eq!(extensions.get::<bool>(), None);
|
assert_eq!(extensions.get::<bool>(), None);
|
||||||
assert_eq!(extensions.get(), Some(&MyType(10)));
|
assert_eq!(extensions.get(), Some(&MyType(10)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extend() {
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct MyType(i32);
|
||||||
|
|
||||||
|
let mut extensions = Extensions::new();
|
||||||
|
|
||||||
|
extensions.insert(5i32);
|
||||||
|
extensions.insert(MyType(10));
|
||||||
|
|
||||||
|
let mut other = Extensions::new();
|
||||||
|
|
||||||
|
other.insert(15i32);
|
||||||
|
other.insert(20u8);
|
||||||
|
|
||||||
|
extensions.extend(other);
|
||||||
|
|
||||||
|
assert_eq!(extensions.get(), Some(&15i32));
|
||||||
|
assert_eq!(extensions.get_mut(), Some(&mut 15i32));
|
||||||
|
|
||||||
|
assert_eq!(extensions.remove::<i32>(), Some(15i32));
|
||||||
|
assert!(extensions.get::<i32>().is_none());
|
||||||
|
|
||||||
|
assert_eq!(extensions.get::<bool>(), None);
|
||||||
|
assert_eq!(extensions.get(), Some(&MyType(10)));
|
||||||
|
|
||||||
|
assert_eq!(extensions.get(), Some(&20u8));
|
||||||
|
assert_eq!(extensions.get_mut(), Some(&mut 20u8));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,6 +183,7 @@ where
|
|||||||
self.data.extend(cfg.data);
|
self.data.extend(cfg.data);
|
||||||
self.services.extend(cfg.services);
|
self.services.extend(cfg.services);
|
||||||
self.external.extend(cfg.external);
|
self.external.extend(cfg.external);
|
||||||
|
self.extensions.extend(cfg.extensions);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,6 +178,7 @@ pub struct ServiceConfig {
|
|||||||
pub(crate) services: Vec<Box<dyn AppServiceFactory>>,
|
pub(crate) services: Vec<Box<dyn AppServiceFactory>>,
|
||||||
pub(crate) data: Vec<Box<dyn DataFactory>>,
|
pub(crate) data: Vec<Box<dyn DataFactory>>,
|
||||||
pub(crate) external: Vec<ResourceDef>,
|
pub(crate) external: Vec<ResourceDef>,
|
||||||
|
pub(crate) extensions: Extensions,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServiceConfig {
|
impl ServiceConfig {
|
||||||
@ -186,6 +187,7 @@ impl ServiceConfig {
|
|||||||
services: Vec::new(),
|
services: Vec::new(),
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
external: Vec::new(),
|
external: Vec::new(),
|
||||||
|
extensions: Extensions::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,6 +200,14 @@ impl ServiceConfig {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set arbitrary data item.
|
||||||
|
///
|
||||||
|
/// This is same as `App::data()` method.
|
||||||
|
pub fn app_data<U: 'static>(&mut self, ext: U) -> &mut Self {
|
||||||
|
self.extensions.insert(ext);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Configure route for a specific path.
|
/// Configure route for a specific path.
|
||||||
///
|
///
|
||||||
/// This is same as `App::route()` method.
|
/// This is same as `App::route()` method.
|
||||||
@ -254,11 +264,14 @@ mod tests {
|
|||||||
async fn test_data() {
|
async fn test_data() {
|
||||||
let cfg = |cfg: &mut ServiceConfig| {
|
let cfg = |cfg: &mut ServiceConfig| {
|
||||||
cfg.data(10usize);
|
cfg.data(10usize);
|
||||||
|
cfg.app_data(15u8);
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut srv =
|
let mut srv = init_service(App::new().configure(cfg).service(
|
||||||
init_service(App::new().configure(cfg).service(
|
web::resource("/").to(|_: web::Data<usize>, req: HttpRequest| {
|
||||||
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
assert_eq!(*req.app_data::<u8>().unwrap(), 15u8);
|
||||||
|
HttpResponse::Ok()
|
||||||
|
}),
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
|
@ -209,6 +209,9 @@ where
|
|||||||
|
|
||||||
self.data = Some(data);
|
self.data = Some(data);
|
||||||
}
|
}
|
||||||
|
self.data
|
||||||
|
.get_or_insert_with(Extensions::new)
|
||||||
|
.extend(cfg.extensions);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user