1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

use better name Route::data instead of Route::config

This commit is contained in:
Nikolay Kim 2019-03-16 11:58:01 -07:00
parent 1f9467e880
commit d93fe157b9
6 changed files with 22 additions and 40 deletions

View File

@ -130,7 +130,7 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
/// web::resource("/index.html") /// web::resource("/index.html")
/// .route(web::get() /// .route(web::get()
/// // change `Form` extractor configuration /// // change `Form` extractor configuration
/// .config(web::FormConfig::default().limit(4097)) /// .data(web::FormConfig::default().limit(4097))
/// .to(index)) /// .to(index))
/// ); /// );
/// } /// }

View File

@ -215,7 +215,7 @@ where
/// fn main() { /// fn main() {
/// let app = App::new().service( /// let app = App::new().service(
/// web::resource("/index.html").route( /// web::resource("/index.html").route(
/// web::post().config( /// web::post().data(
/// // change json extractor configuration /// // change json extractor configuration
/// web::JsonConfig::default().limit(4096) /// web::JsonConfig::default().limit(4096)
/// .error_handler(|err, req| { // <- create custom error response /// .error_handler(|err, req| { // <- create custom error response

View File

@ -262,7 +262,7 @@ mod tests {
header::CONTENT_TYPE, header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.config(FormConfig::default().limit(4096)) .route_data(FormConfig::default().limit(4096))
.to_from(); .to_from();
let r = block_on(Option::<Form<Info>>::from_request(&mut req)).unwrap(); let r = block_on(Option::<Form<Info>>::from_request(&mut req)).unwrap();

View File

@ -177,7 +177,7 @@ where
/// let app = App::new().service( /// let app = App::new().service(
/// web::resource("/index.html").route( /// web::resource("/index.html").route(
/// web::get() /// web::get()
/// .config(web::PayloadConfig::new(4096)) // <- limit size of the payload /// .data(web::PayloadConfig::new(4096)) // <- limit size of the payload
/// .to(index)) // <- register handler with extractor params /// .to(index)) // <- register handler with extractor params
/// ); /// );
/// } /// }

View File

@ -40,28 +40,28 @@ type BoxedRouteNewService<Req, Res> = Box<
pub struct Route<P> { pub struct Route<P> {
service: BoxedRouteNewService<ServiceRequest<P>, ServiceResponse>, service: BoxedRouteNewService<ServiceRequest<P>, ServiceResponse>,
guards: Rc<Vec<Box<Guard>>>, guards: Rc<Vec<Box<Guard>>>,
config: Option<Extensions>, data: Option<Extensions>,
config_ref: Rc<RefCell<Option<Rc<Extensions>>>>, data_ref: Rc<RefCell<Option<Rc<Extensions>>>>,
} }
impl<P: 'static> Route<P> { impl<P: 'static> Route<P> {
/// Create new route which matches any request. /// Create new route which matches any request.
pub fn new() -> Route<P> { pub fn new() -> Route<P> {
let config_ref = Rc::new(RefCell::new(None)); let data_ref = Rc::new(RefCell::new(None));
Route { Route {
service: Box::new(RouteNewService::new( service: Box::new(RouteNewService::new(
Extract::new(config_ref.clone()).and_then( Extract::new(data_ref.clone()).and_then(
Handler::new(HttpResponse::NotFound).map_err(|_| panic!()), Handler::new(HttpResponse::NotFound).map_err(|_| panic!()),
), ),
)), )),
guards: Rc::new(Vec::new()), guards: Rc::new(Vec::new()),
config: None, data: None,
config_ref, data_ref,
} }
} }
pub(crate) fn finish(mut self) -> Self { pub(crate) fn finish(mut self) -> Self {
*self.config_ref.borrow_mut() = self.config.take().map(|e| Rc::new(e)); *self.data_ref.borrow_mut() = self.data.take().map(|e| Rc::new(e));
self self
} }
@ -180,24 +180,6 @@ impl<P: 'static> Route<P> {
self self
} }
// pub fn map<T, U, F: IntoNewService<T>>(
// self,
// md: F,
// ) -> RouteServiceBuilder<T, S, (), U>
// where
// T: NewService<
// Request = HandlerRequest<S>,
// Response = HandlerRequest<S, U>,
// InitError = Error,
// >,
// {
// RouteServiceBuilder {
// service: md.into_new_service(),
// guards: self.guards,
// _t: PhantomData,
// }
// }
/// Set handler function, use request extractors for parameters. /// Set handler function, use request extractors for parameters.
/// ///
/// ```rust /// ```rust
@ -253,7 +235,7 @@ impl<P: 'static> Route<P> {
R: Responder + 'static, R: Responder + 'static,
{ {
self.service = Box::new(RouteNewService::new( self.service = Box::new(RouteNewService::new(
Extract::new(self.config_ref.clone()) Extract::new(self.data_ref.clone())
.and_then(Handler::new(handler).map_err(|_| panic!())), .and_then(Handler::new(handler).map_err(|_| panic!())),
)); ));
self self
@ -295,14 +277,14 @@ impl<P: 'static> Route<P> {
R::Error: Into<Error>, R::Error: Into<Error>,
{ {
self.service = Box::new(RouteNewService::new( self.service = Box::new(RouteNewService::new(
Extract::new(self.config_ref.clone()) Extract::new(self.data_ref.clone())
.and_then(AsyncHandler::new(handler).map_err(|_| panic!())), .and_then(AsyncHandler::new(handler).map_err(|_| panic!())),
)); ));
self self
} }
/// This method allows to add extractor configuration /// Provide route specific data. This method allows to add extractor
/// for specific route. /// configuration or specific state available via `RouteData<T>` extractor.
/// ///
/// ```rust /// ```rust
/// use actix_web::{web, App}; /// use actix_web::{web, App};
@ -317,17 +299,17 @@ impl<P: 'static> Route<P> {
/// web::resource("/index.html").route( /// web::resource("/index.html").route(
/// web::get() /// web::get()
/// // limit size of the payload /// // limit size of the payload
/// .config(web::PayloadConfig::new(4096)) /// .data(web::PayloadConfig::new(4096))
/// // register handler /// // register handler
/// .to(index) /// .to(index)
/// )); /// ));
/// } /// }
/// ``` /// ```
pub fn config<C: 'static>(mut self, config: C) -> Self { pub fn data<C: 'static>(mut self, data: C) -> Self {
if self.config.is_none() { if self.data.is_none() {
self.config = Some(Extensions::new()); self.data = Some(Extensions::new());
} }
self.config.as_mut().unwrap().insert(config); self.data.as_mut().unwrap().insert(data);
self self
} }
} }

View File

@ -265,8 +265,8 @@ impl TestRequest {
self self
} }
/// Set request config /// Set route data
pub fn config<T: 'static>(self, data: T) -> Self { pub fn route_data<T: 'static>(self, data: T) -> Self {
self.config.extensions.borrow_mut().insert(data); self.config.extensions.borrow_mut().insert(data);
self self
} }