diff --git a/CHANGES.md b/CHANGES.md index 4aaef9511..7a6e9b9bf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,11 @@ * `App::configure()` allow to offload app configuration to different methods +* Added `ServiceRequest::app_data()`, returns `Data` + +* Added `ServiceFromRequest::app_data()`, returns `Data` + + ### Changed * Move multipart support to actix-multipart crate diff --git a/src/service.rs b/src/service.rs index 13aae8692..0f11b89e1 100644 --- a/src/service.rs +++ b/src/service.rs @@ -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

ServiceRequest

{ 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(&self) -> Option> { + if let Some(st) = self.req.config().extensions().get::>() { + Some(st.clone()) + } else { + None + } + } } impl

Resource for ServiceRequest

{ @@ -270,6 +280,16 @@ impl

ServiceFromRequest

{ ServiceResponse::new(self.req, err.into().into()) } + /// Get an application data stored with `App::data()` method during + /// application configuration. + pub fn app_data(&self) -> Option> { + if let Some(st) = self.req.config().extensions().get::>() { + Some(st.clone()) + } else { + None + } + } + /// Load route data. Route data could be set during /// route configuration with `Route::data()` method. pub fn route_data(&self) -> Option<&RouteData> {