mirror of
https://github.com/fafhrd91/actix-web
synced 2025-07-01 16:55:08 +02:00
unify route and app data, it allows to provide global extractor config #775
This commit is contained in:
189
src/data.rs
189
src/data.rs
@ -3,7 +3,6 @@ use std::sync::Arc;
|
||||
|
||||
use actix_http::error::{Error, ErrorInternalServerError};
|
||||
use actix_http::Extensions;
|
||||
use futures::{Async, Future, IntoFuture, Poll};
|
||||
|
||||
use crate::dev::Payload;
|
||||
use crate::extract::FromRequest;
|
||||
@ -11,11 +10,7 @@ use crate::request::HttpRequest;
|
||||
|
||||
/// Application data factory
|
||||
pub(crate) trait DataFactory {
|
||||
fn construct(&self) -> Box<DataFactoryResult>;
|
||||
}
|
||||
|
||||
pub(crate) trait DataFactoryResult {
|
||||
fn poll_result(&mut self, extensions: &mut Extensions) -> Poll<(), ()>;
|
||||
fn create(&self, extensions: &mut Extensions) -> bool;
|
||||
}
|
||||
|
||||
/// Application data.
|
||||
@ -111,8 +106,8 @@ impl<T: 'static> FromRequest for Data<T> {
|
||||
|
||||
#[inline]
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
if let Some(st) = req.app_config().extensions().get::<Data<T>>() {
|
||||
Ok(st.clone())
|
||||
if let Some(st) = req.get_app_data::<T>() {
|
||||
Ok(st)
|
||||
} else {
|
||||
log::debug!(
|
||||
"Failed to construct App-level Data extractor. \
|
||||
@ -127,142 +122,12 @@ impl<T: 'static> FromRequest for Data<T> {
|
||||
}
|
||||
|
||||
impl<T: 'static> DataFactory for Data<T> {
|
||||
fn construct(&self) -> Box<DataFactoryResult> {
|
||||
Box::new(DataFut { st: self.clone() })
|
||||
}
|
||||
}
|
||||
|
||||
struct DataFut<T> {
|
||||
st: Data<T>,
|
||||
}
|
||||
|
||||
impl<T: 'static> DataFactoryResult for DataFut<T> {
|
||||
fn poll_result(&mut self, extensions: &mut Extensions) -> Poll<(), ()> {
|
||||
extensions.insert(self.st.clone());
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, Out> DataFactory for F
|
||||
where
|
||||
F: Fn() -> Out + 'static,
|
||||
Out: IntoFuture + 'static,
|
||||
Out::Error: std::fmt::Debug,
|
||||
{
|
||||
fn construct(&self) -> Box<DataFactoryResult> {
|
||||
Box::new(DataFactoryFut {
|
||||
fut: (*self)().into_future(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct DataFactoryFut<T, F>
|
||||
where
|
||||
F: Future<Item = T>,
|
||||
F::Error: std::fmt::Debug,
|
||||
{
|
||||
fut: F,
|
||||
}
|
||||
|
||||
impl<T: 'static, F> DataFactoryResult for DataFactoryFut<T, F>
|
||||
where
|
||||
F: Future<Item = T>,
|
||||
F::Error: std::fmt::Debug,
|
||||
{
|
||||
fn poll_result(&mut self, extensions: &mut Extensions) -> Poll<(), ()> {
|
||||
match self.fut.poll() {
|
||||
Ok(Async::Ready(s)) => {
|
||||
extensions.insert(Data::new(s));
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
||||
Err(e) => {
|
||||
log::error!("Can not construct application state: {:?}", e);
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Route data.
|
||||
///
|
||||
/// Route data is an arbitrary data attached to specific route.
|
||||
/// Route data could be added to route during route configuration process
|
||||
/// with `Route::data()` method. Route data is also used as an extractor
|
||||
/// configuration storage. Route data could be accessed in handler
|
||||
/// via `RouteData<T>` extractor.
|
||||
///
|
||||
/// If route data is not set for a handler, using `RouteData` extractor
|
||||
/// would cause *Internal Server Error* response.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use std::cell::Cell;
|
||||
/// use actix_web::{web, App};
|
||||
///
|
||||
/// struct MyData {
|
||||
/// counter: Cell<usize>,
|
||||
/// }
|
||||
///
|
||||
/// /// Use `RouteData<T>` extractor to access data in handler.
|
||||
/// fn index(data: web::RouteData<MyData>) {
|
||||
/// data.counter.set(data.counter.get() + 1);
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().service(
|
||||
/// web::resource("/index.html").route(
|
||||
/// web::get()
|
||||
/// // Store `MyData` in route storage
|
||||
/// .data(MyData{ counter: Cell::new(0) })
|
||||
/// // Route data could be used as extractor configuration storage,
|
||||
/// // limit size of the payload
|
||||
/// .data(web::PayloadConfig::new(4096))
|
||||
/// // register handler
|
||||
/// .to(index)
|
||||
/// ));
|
||||
/// }
|
||||
/// ```
|
||||
pub struct RouteData<T>(Arc<T>);
|
||||
|
||||
impl<T> RouteData<T> {
|
||||
pub(crate) fn new(state: T) -> RouteData<T> {
|
||||
RouteData(Arc::new(state))
|
||||
}
|
||||
|
||||
/// Get referecnce to inner data object.
|
||||
pub fn get_ref(&self) -> &T {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for RouteData<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &T {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for RouteData<T> {
|
||||
fn clone(&self) -> RouteData<T> {
|
||||
RouteData(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> FromRequest for RouteData<T> {
|
||||
type Config = ();
|
||||
type Error = Error;
|
||||
type Future = Result<Self, Error>;
|
||||
|
||||
#[inline]
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
if let Some(st) = req.route_data::<T>() {
|
||||
Ok(st.clone())
|
||||
fn create(&self, extensions: &mut Extensions) -> bool {
|
||||
if !extensions.contains::<Data<T>>() {
|
||||
let _ = extensions.insert(Data(self.0.clone()));
|
||||
true
|
||||
} else {
|
||||
log::debug!("Failed to construct Route-level Data extractor");
|
||||
Err(ErrorInternalServerError(
|
||||
"Route data is not configured, to configure use Route::data()",
|
||||
))
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -297,12 +162,13 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_route_data_extractor() {
|
||||
let mut srv = init_service(App::new().service(web::resource("/").route(
|
||||
web::get().data(10usize).to(|data: web::RouteData<usize>| {
|
||||
let _ = data.clone();
|
||||
HttpResponse::Ok()
|
||||
}),
|
||||
)));
|
||||
let mut srv =
|
||||
init_service(App::new().service(web::resource("/").data(10usize).route(
|
||||
web::get().to(|data: web::Data<usize>| {
|
||||
let _ = data.clone();
|
||||
HttpResponse::Ok()
|
||||
}),
|
||||
)));
|
||||
|
||||
let req = TestRequest::default().to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
@ -311,15 +177,30 @@ mod tests {
|
||||
// different type
|
||||
let mut srv = init_service(
|
||||
App::new().service(
|
||||
web::resource("/").route(
|
||||
web::get()
|
||||
.data(10u32)
|
||||
.to(|_: web::RouteData<usize>| HttpResponse::Ok()),
|
||||
),
|
||||
web::resource("/")
|
||||
.data(10u32)
|
||||
.route(web::get().to(|_: web::Data<usize>| HttpResponse::Ok())),
|
||||
),
|
||||
);
|
||||
let req = TestRequest::default().to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_override_data() {
|
||||
let mut srv = init_service(App::new().data(1usize).service(
|
||||
web::resource("/").data(10usize).route(web::get().to(
|
||||
|data: web::Data<usize>| {
|
||||
assert_eq!(*data, 10);
|
||||
let _ = data.clone();
|
||||
HttpResponse::Ok()
|
||||
},
|
||||
)),
|
||||
));
|
||||
|
||||
let req = TestRequest::default().to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user