From 87c7441f7df028063ffdf839b2f5fef6194f7966 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 6 Dec 2017 08:03:08 -0800 Subject: [PATCH] remove Applicaiton::route, resource is enough --- examples/basic.rs | 12 ++++++------ examples/state.rs | 5 ++--- examples/websocket.rs | 4 ++-- guide/src/qs_12.md | 2 +- guide/src/qs_2.md | 2 +- guide/src/qs_3.md | 6 +++--- guide/src/qs_4.md | 4 ++-- guide/src/qs_5.md | 6 +++--- src/application.rs | 35 ----------------------------------- src/fs.rs | 2 +- src/resource.rs | 33 ++++++++++++++++++++++++++++++++- 11 files changed, 53 insertions(+), 58 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index e14b36b86..db43fce50 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -67,13 +67,13 @@ fn main() { .finish() )) // register simple route, handle all methods - .route("/index.html", |r| r.f(index)) + .resource("/index.html", |r| r.f(index)) // with path parameters - .resource("/user/{name}/", |r| r.route().method(Method::GET).f(with_param)) + .resource("/user/{name}/", |r| r.method(Method::GET).f(with_param)) // async handler - .resource("/async/{name}", |r| r.route().method(Method::GET).a(index_async)) + .resource("/async/{name}", |r| r.method(Method::GET).a(index_async)) // redirect - .resource("/", |r| r.route().method(Method::GET).f(|req| { + .resource("/", |r| r.method(Method::GET).f(|req| { println!("{:?}", req); httpcodes::HTTPFound @@ -81,7 +81,7 @@ fn main() { .header("LOCATION", "/index.html") .body(Body::Empty) })) - .route("/test", |r| r.f(|req| { + .resource("/test", |r| r.f(|req| { match *req.method() { Method::GET => httpcodes::HTTPOk, Method::POST => httpcodes::HTTPMethodNotAllowed, @@ -89,7 +89,7 @@ fn main() { } })) // static files - .route("/static", |r| r.h(fs::StaticFiles::new("examples/static/", true)))) + .resource("/static", |r| r.h(fs::StaticFiles::new("examples/static/", true)))) .serve::<_, ()>("127.0.0.1:8080").unwrap(); println!("Started http server: 127.0.0.1:8080"); diff --git a/examples/state.rs b/examples/state.rs index f7e980413..2e8df9256 100644 --- a/examples/state.rs +++ b/examples/state.rs @@ -65,11 +65,10 @@ fn main() { .middleware(middlewares::Logger::default()) // websocket route .resource( - "/ws/", |r| r.route() - .method(Method::GET) + "/ws/", |r| r.method(Method::GET) .f(|req| ws::start(req, MyWebSocket{counter: 0}))) // register simple handler, handle all methods - .route("/", |r| r.f(index))) + .resource("/", |r| r.f(index))) .serve::<_, ()>("127.0.0.1:8080").unwrap(); println!("Started http server: 127.0.0.1:8080"); diff --git a/examples/websocket.rs b/examples/websocket.rs index 0187f0c08..ba316b601 100644 --- a/examples/websocket.rs +++ b/examples/websocket.rs @@ -65,9 +65,9 @@ fn main() { // enable logger .middleware(middlewares::Logger::default()) // websocket route - .resource("/ws/", |r| r.route().method(Method::GET).f(ws_index)) + .resource("/ws/", |r| r.method(Method::GET).f(ws_index)) // static files - .route("/", |r| r.h(fs::StaticFiles::new("examples/static/", true)))) + .resource("/", |r| r.h(fs::StaticFiles::new("examples/static/", true)))) // start http server on 127.0.0.1:8080 .serve::<_, ()>("127.0.0.1:8080").unwrap(); diff --git a/guide/src/qs_12.md b/guide/src/qs_12.md index c16ac0f30..4cfa8b5f4 100644 --- a/guide/src/qs_12.md +++ b/guide/src/qs_12.md @@ -33,7 +33,7 @@ use actix_web::*; fn main() { Application::default("/") - .route("/static", |r| r.h(fs::StaticFiles::new(".", true))) + .resource("/static", |r| r.h(fs::StaticFiles::new(".", true))) .finish(); } ``` diff --git a/guide/src/qs_2.md b/guide/src/qs_2.md index b8179db35..41c62a6f1 100644 --- a/guide/src/qs_2.md +++ b/guide/src/qs_2.md @@ -80,7 +80,7 @@ fn main() { HttpServer::new( Application::default("/") - .resource("/", |r| r.route().f(index))) + .resource("/", |r| r.f(index))) .serve::<_, ()>("127.0.0.1:8088").unwrap(); println!("Started http server: 127.0.0.1:8088"); diff --git a/guide/src/qs_3.md b/guide/src/qs_3.md index f94e27266..e70e04bef 100644 --- a/guide/src/qs_3.md +++ b/guide/src/qs_3.md @@ -41,13 +41,13 @@ use tokio_core::net::TcpStream; fn main() { HttpServer::::new(vec![ Application::default("/app1") - .resource("/", |r| r.route().f(|r| httpcodes::HTTPOk)) + .resource("/", |r| r.f(|r| httpcodes::HTTPOk)) .finish(), Application::default("/app2") - .resource("/", |r| r.route().f(|r| httpcodes::HTTPOk)) + .resource("/", |r| r.f(|r| httpcodes::HTTPOk)) .finish(), Application::default("/") - .resource("/", |r| r.route().f(|r| httpcodes::HTTPOk)) + .resource("/", |r| r.f(|r| httpcodes::HTTPOk)) .finish(), ]); } diff --git a/guide/src/qs_4.md b/guide/src/qs_4.md index c8683591d..bd37434b2 100644 --- a/guide/src/qs_4.md +++ b/guide/src/qs_4.md @@ -114,7 +114,7 @@ fn index(req: HttpRequest) -> FutureResult { fn main() { Application::default("/") - .route("/async", |r| r.a(index)) + .resource("/async", |r| r.route().a(index)) .finish(); } ``` @@ -139,7 +139,7 @@ fn index(req: HttpRequest) -> HttpResponse { fn main() { Application::default("/") - .route("/async", |r| r.f(index)) + .resource("/async", |r| r.f(index)) .finish(); } ``` diff --git a/guide/src/qs_5.md b/guide/src/qs_5.md index a03bf91ee..b2ae22753 100644 --- a/guide/src/qs_5.md +++ b/guide/src/qs_5.md @@ -18,7 +18,7 @@ fn index(req: HttpRequest) -> HttpResponse { fn main() { Application::default("/") - .route("/prefix", |r| r.f(index)) + .resource("/prefix", |r| r.f(index)) .finish(); } ``` @@ -37,7 +37,7 @@ fn index(req: HttpRequest) -> HttpResponse { fn main() { Application::default("/app") - .route("/prefix", |r| r.f(index)) + .resource("/prefix", |r| r.f(index)) .finish(); } ``` @@ -126,7 +126,7 @@ fn index(req: HttpRequest) -> Result { fn main() { Application::default("/") - .resource(r"/a/{v1}/{v2}/", |r| r.route().f(index)) + .resource(r"/a/{v1}/{v2}/", |r| r.f(index)) .finish(); } ``` diff --git a/src/application.rs b/src/application.rs index 5db0be5d1..d0274725a 100644 --- a/src/application.rs +++ b/src/application.rs @@ -210,41 +210,6 @@ impl ApplicationBuilder where S: 'static { self } - /// This method register route for specified path prefix. - /// Route maches based on path prefix, variable path patterns are not available - /// in this case. If you need variable path patterns consider using *resource()* - /// method. - /// - /// ```rust - /// extern crate actix_web; - /// use actix_web::*; - /// - /// fn main() { - /// let app = Application::default("/") - /// .route("/test", |r| r.f( - /// |req| { - /// match *req.method() { - /// Method::GET => httpcodes::HTTPOk, - /// Method::POST => httpcodes::HTTPMethodNotAllowed, - /// _ => httpcodes::HTTPNotFound, - /// } - /// } - /// )) - /// .finish(); - /// } - /// ``` - pub fn route>(&mut self, path: P, f: F) -> &mut Self - where P: Into, - F: FnOnce(&mut Route) + 'static - { - { - let parts = self.parts.as_mut().expect("Use after finish"); - parts.routes.push((path.into(), Route::default())); - f(&mut parts.routes.last_mut().unwrap().1); - } - self - } - /// Register a middleware pub fn middleware(&mut self, mw: T) -> &mut Self where T: Middleware + 'static diff --git a/src/fs.rs b/src/fs.rs index 1bf678eea..7f0a2a2dc 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -198,7 +198,7 @@ impl FromRequest for FilesystemElement { /// /// fn main() { /// let app = actix_web::Application::default("/") -/// .route("/static", |r| r.h(actix_web::fs::StaticFiles::new(".", true))) +/// .resource("/static", |r| r.h(actix_web::fs::StaticFiles::new(".", true))) /// .finish(); /// } /// ``` diff --git a/src/resource.rs b/src/resource.rs index 28fc1a0cf..f499c42b1 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -3,7 +3,7 @@ use std::marker::PhantomData; use http::Method; use route::Route; -use handler::{Reply, Handler, RouteHandler, WrapHandler}; +use handler::{Reply, Handler, FromRequest, RouteHandler, WrapHandler}; use httpcodes::HTTPNotFound; use httprequest::HttpRequest; @@ -79,17 +79,48 @@ impl Resource where S: 'static { /// .f(|r| HttpResponse::Ok())) /// .finish(); /// } + /// ``` pub fn route(&mut self) -> &mut Route { self.routes.push(Route::default()); self.routes.last_mut().unwrap() } /// Register a new route and add method check to route. + /// + /// This is sortcut for: + /// ```rust,ignore + /// Resource::resource("/", |r| r.route().method(Method::GET).f(index) + /// ``` pub fn method(&mut self, method: Method) -> &mut Route { self.routes.push(Route::default()); self.routes.last_mut().unwrap().method(method) } + /// Register a new route and add handler object. + /// + /// This is sortcut for: + /// ```rust,ignore + /// Resource::resource("/", |r| r.route().h(handler) + /// ``` + pub fn h>(&mut self, handler: H) { + self.routes.push(Route::default()); + self.routes.last_mut().unwrap().h(handler) + } + + /// Register a new route and add handler function. + /// + /// This is sortcut for: + /// ```rust,ignore + /// Resource::resource("/", |r| r.route().f(index) + /// ``` + pub fn f(&mut self, handler: F) + where F: Fn(HttpRequest) -> R + 'static, + R: FromRequest + 'static, + { + self.routes.push(Route::default()); + self.routes.last_mut().unwrap().f(handler) + } + /// Default handler is used if no matched route found. /// By default `HTTPNotFound` is used. pub fn default_handler(&mut self, handler: H) where H: Handler {