From 62fb75ff955f4c9b6d1c46525cdf6b6263a0f298 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 27 Mar 2018 11:16:02 -0700 Subject: [PATCH] add Application::configure method, it simplifies configuration process --- src/application.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/application.rs b/src/application.rs index 4588659b0..86b16cd25 100644 --- a/src/application.rs +++ b/src/application.rs @@ -344,6 +344,38 @@ impl Application where S: 'static { self } + /// Run external configuration as part of application building process + /// + /// This function is useful for moving part of configuration to a different + /// module or event library. For example we can move some of the resources + /// configuration to different module. + /// + /// ```rust + /// # extern crate actix_web; + /// use actix_web::*; + /// + /// // this function could be located in different module + /// fn config(app: Application) -> Application { + /// app + /// .resource("/test", |r| { + /// r.method(Method::GET).f(|_| httpcodes::HttpOk); + /// r.method(Method::HEAD).f(|_| httpcodes::HttpMethodNotAllowed); + /// }) + /// } + /// + /// fn main() { + /// let app = Application::new() + /// .middleware(middleware::Logger::default()) + /// .configure(config) // <- register resources + /// .handler("/static", fs::StaticFiles::new(".", true)); + /// } + /// ``` + pub fn configure(self, cfg: F) -> Application + where F: Fn(Application) -> Application + { + cfg(self) + } + /// Finish application configuration and create HttpHandler object pub fn finish(&mut self) -> HttpApplication { let parts = self.parts.take().expect("Use after finish");