1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-23 16:21:06 +01:00

added app_data() method

This commit is contained in:
Nikolay Kim 2019-04-03 15:25:52 -07:00
parent 237bfba1ed
commit cef3dc3586
2 changed files with 26 additions and 1 deletions

View File

@ -6,6 +6,11 @@
* `App::configure()` allow to offload app configuration to different methods
* Added `ServiceRequest::app_data()`, returns `Data<T>`
* Added `ServiceFromRequest::app_data()`, returns `Data<T>`
### Changed
* Move multipart support to actix-multipart crate

View File

@ -13,7 +13,7 @@ use actix_router::{Path, Resource, Url};
use futures::future::{ok, FutureResult, IntoFuture};
use crate::config::{AppConfig, ServiceConfig};
use crate::data::RouteData;
use crate::data::{Data, RouteData};
use crate::request::HttpRequest;
use crate::rmap::ResourceMap;
@ -173,6 +173,16 @@ impl<P> ServiceRequest<P> {
pub fn app_config(&self) -> &AppConfig {
self.req.config()
}
/// Get an application data stored with `App::data()` method during
/// application configuration.
pub fn app_data<T: 'static>(&self) -> Option<Data<T>> {
if let Some(st) = self.req.config().extensions().get::<Data<T>>() {
Some(st.clone())
} else {
None
}
}
}
impl<P> Resource<Url> for ServiceRequest<P> {
@ -270,6 +280,16 @@ impl<P> ServiceFromRequest<P> {
ServiceResponse::new(self.req, err.into().into())
}
/// Get an application data stored with `App::data()` method during
/// application configuration.
pub fn app_data<T: 'static>(&self) -> Option<Data<T>> {
if let Some(st) = self.req.config().extensions().get::<Data<T>>() {
Some(st.clone())
} else {
None
}
}
/// Load route data. Route data could be set during
/// route configuration with `Route::data()` method.
pub fn route_data<T: 'static>(&self) -> Option<&RouteData<T>> {