diff --git a/CHANGES.md b/CHANGES.md index fc690ee50..4aaef9511 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,10 @@ # Changes -## [1.0.0-alpha.3] - 2019-04-xx +## [1.0.0-alpha.4] - 2019-04-xx + +### Added + +* `App::configure()` allow to offload app configuration to different methods ### Changed diff --git a/actix-multipart/README.md b/actix-multipart/README.md index 2a65840a1..2739ff3d5 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -1 +1 @@ -# Multipart support for actix web framework [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![crates.io](https://meritbadge.herokuapp.com/actix-session)](https://crates.io/crates/actix-session) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +# Multipart support for actix web framework [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![crates.io](https://meritbadge.herokuapp.com/actix-multipart)](https://crates.io/crates/actix-multipart) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) diff --git a/src/app.rs b/src/app.rs index fd91d0728..802569458 100644 --- a/src/app.rs +++ b/src/app.rs @@ -15,7 +15,7 @@ use bytes::Bytes; use futures::{IntoFuture, Stream}; use crate::app_service::{AppChain, AppEntry, AppInit, AppRouting, AppRoutingFactory}; -use crate::config::{AppConfig, AppConfigInner}; +use crate::config::{AppConfig, AppConfigInner, RouterConfig}; use crate::data::{Data, DataFactory}; use crate::dev::{Payload, PayloadStream, ResourceDef}; use crate::error::{Error, PayloadError}; @@ -257,6 +257,55 @@ where } } + /// Run external configuration as part of the application building + /// process + /// + /// This function is useful for moving parts of configuration to a + /// different module or even library. For example, + /// some of the resource's configuration could be moved to different module. + /// + /// ```rust + /// # extern crate actix_web; + /// use actix_web::{web, middleware, App, HttpResponse}; + /// + /// // this function could be located in different module + /// fn config
(cfg: &mut web::RouterConfig
) {
+ /// cfg.service(web::resource("/test")
+ /// .route(web::get().to(|| HttpResponse::Ok()))
+ /// .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
+ /// );
+ /// }
+ ///
+ /// fn main() {
+ /// let app = App::new()
+ /// .wrap(middleware::Logger::default())
+ /// .configure(config) // <- register resources
+ /// .route("/index.html", web::get().to(|| HttpResponse::Ok()));
+ /// }
+ /// ```
+ pub fn configure (cfg: &mut web::RouterConfig ) {
+ /// cfg.service(web::resource("/test")
+ /// .route(web::get().to(|| HttpResponse::Ok()))
+ /// .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
+ /// );
+ /// }
+ ///
+ /// fn main() {
+ /// let app = App::new()
+ /// .wrap(middleware::Logger::default())
+ /// .configure(config) // <- register resources
+ /// .route("/index.html", web::get().to(|| HttpResponse::Ok()));
+ /// }
+ /// ```
+ pub fn configure ),
+ {
+ let mut cfg = RouterConfig::new();
+ f(&mut cfg);
+ self.data.extend(cfg.data);
+ self.services.extend(cfg.services);
+ self.external.extend(cfg.external);
+
+ self
+ }
+
/// Configure route for a specific path.
///
/// This is a simplified version of the `App::service()` method.
diff --git a/src/config.rs b/src/config.rs
index ceb58feb7..1e552291f 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -5,11 +5,18 @@ use std::rc::Rc;
use actix_http::Extensions;
use actix_router::ResourceDef;
use actix_service::{boxed, IntoNewService, NewService};
+use futures::IntoFuture;
+use crate::data::{Data, DataFactory};
use crate::error::Error;
use crate::guard::Guard;
+use crate::resource::Resource;
use crate::rmap::ResourceMap;
-use crate::service::{ServiceRequest, ServiceResponse};
+use crate::route::Route;
+use crate::service::{
+ HttpServiceFactory, ServiceFactory, ServiceFactoryWrapper, ServiceRequest,
+ ServiceResponse,
+};
type Guards = Vec =
@@ -157,3 +164,140 @@ impl Default for AppConfigInner {
}
}
}
+
+/// Router config. It is used for external configuration.
+/// Part of application configuration could be offloaded
+/// to set of external methods. This could help with
+/// modularization of big application configuration.
+pub struct RouterConfig {
+ pub(crate) fn new() -> Self {
+ Self {
+ services: Vec::new(),
+ data: Vec::new(),
+ external: Vec::new(),
+ }
+ }
+
+ /// Set application data. Applicatin data could be accessed
+ /// by using `Data ) -> &mut Self {
+ self.service(
+ Resource::new(path)
+ .add_guards(route.take_guards())
+ .route(route),
+ )
+ }
+
+ /// Register http service.
+ ///
+ /// This is same as `App::service()` method.
+ pub fn service + 'static,
+ {
+ self.services
+ .push(Box::new(ServiceFactoryWrapper::new(factory)));
+ self
+ }
+
+ /// Register an external resource.
+ ///
+ /// External resources are useful for URL generation purposes only
+ /// and are never considered for matching at request time. Calls to
+ /// `HttpRequest::url_for()` will work as expected.
+ ///
+ /// This is same as `App::external_service()` method.
+ pub fn external_resource