1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00

remove Applicaiton::route, resource is enough

This commit is contained in:
Nikolay Kim 2017-12-06 08:03:08 -08:00
parent 04ded5ba68
commit 87c7441f7d
11 changed files with 53 additions and 58 deletions

View File

@ -67,13 +67,13 @@ fn main() {
.finish() .finish()
)) ))
// register simple route, handle all methods // register simple route, handle all methods
.route("/index.html", |r| r.f(index)) .resource("/index.html", |r| r.f(index))
// with path parameters // 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 // 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 // redirect
.resource("/", |r| r.route().method(Method::GET).f(|req| { .resource("/", |r| r.method(Method::GET).f(|req| {
println!("{:?}", req); println!("{:?}", req);
httpcodes::HTTPFound httpcodes::HTTPFound
@ -81,7 +81,7 @@ fn main() {
.header("LOCATION", "/index.html") .header("LOCATION", "/index.html")
.body(Body::Empty) .body(Body::Empty)
})) }))
.route("/test", |r| r.f(|req| { .resource("/test", |r| r.f(|req| {
match *req.method() { match *req.method() {
Method::GET => httpcodes::HTTPOk, Method::GET => httpcodes::HTTPOk,
Method::POST => httpcodes::HTTPMethodNotAllowed, Method::POST => httpcodes::HTTPMethodNotAllowed,
@ -89,7 +89,7 @@ fn main() {
} }
})) }))
// static files // 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(); .serve::<_, ()>("127.0.0.1:8080").unwrap();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");

View File

@ -65,11 +65,10 @@ fn main() {
.middleware(middlewares::Logger::default()) .middleware(middlewares::Logger::default())
// websocket route // websocket route
.resource( .resource(
"/ws/", |r| r.route() "/ws/", |r| r.method(Method::GET)
.method(Method::GET)
.f(|req| ws::start(req, MyWebSocket{counter: 0}))) .f(|req| ws::start(req, MyWebSocket{counter: 0})))
// register simple handler, handle all methods // register simple handler, handle all methods
.route("/", |r| r.f(index))) .resource("/", |r| r.f(index)))
.serve::<_, ()>("127.0.0.1:8080").unwrap(); .serve::<_, ()>("127.0.0.1:8080").unwrap();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");

View File

@ -65,9 +65,9 @@ fn main() {
// enable logger // enable logger
.middleware(middlewares::Logger::default()) .middleware(middlewares::Logger::default())
// websocket route // 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 // 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 // start http server on 127.0.0.1:8080
.serve::<_, ()>("127.0.0.1:8080").unwrap(); .serve::<_, ()>("127.0.0.1:8080").unwrap();

View File

@ -33,7 +33,7 @@ use actix_web::*;
fn main() { fn main() {
Application::default("/") Application::default("/")
.route("/static", |r| r.h(fs::StaticFiles::new(".", true))) .resource("/static", |r| r.h(fs::StaticFiles::new(".", true)))
.finish(); .finish();
} }
``` ```

View File

@ -80,7 +80,7 @@ fn main() {
HttpServer::new( HttpServer::new(
Application::default("/") Application::default("/")
.resource("/", |r| r.route().f(index))) .resource("/", |r| r.f(index)))
.serve::<_, ()>("127.0.0.1:8088").unwrap(); .serve::<_, ()>("127.0.0.1:8088").unwrap();
println!("Started http server: 127.0.0.1:8088"); println!("Started http server: 127.0.0.1:8088");

View File

@ -41,13 +41,13 @@ use tokio_core::net::TcpStream;
fn main() { fn main() {
HttpServer::<TcpStream, SocketAddr, _>::new(vec![ HttpServer::<TcpStream, SocketAddr, _>::new(vec![
Application::default("/app1") Application::default("/app1")
.resource("/", |r| r.route().f(|r| httpcodes::HTTPOk)) .resource("/", |r| r.f(|r| httpcodes::HTTPOk))
.finish(), .finish(),
Application::default("/app2") Application::default("/app2")
.resource("/", |r| r.route().f(|r| httpcodes::HTTPOk)) .resource("/", |r| r.f(|r| httpcodes::HTTPOk))
.finish(), .finish(),
Application::default("/") Application::default("/")
.resource("/", |r| r.route().f(|r| httpcodes::HTTPOk)) .resource("/", |r| r.f(|r| httpcodes::HTTPOk))
.finish(), .finish(),
]); ]);
} }

View File

@ -114,7 +114,7 @@ fn index(req: HttpRequest) -> FutureResult<HttpResponse, Error> {
fn main() { fn main() {
Application::default("/") Application::default("/")
.route("/async", |r| r.a(index)) .resource("/async", |r| r.route().a(index))
.finish(); .finish();
} }
``` ```
@ -139,7 +139,7 @@ fn index(req: HttpRequest) -> HttpResponse {
fn main() { fn main() {
Application::default("/") Application::default("/")
.route("/async", |r| r.f(index)) .resource("/async", |r| r.f(index))
.finish(); .finish();
} }
``` ```

View File

@ -18,7 +18,7 @@ fn index(req: HttpRequest) -> HttpResponse {
fn main() { fn main() {
Application::default("/") Application::default("/")
.route("/prefix", |r| r.f(index)) .resource("/prefix", |r| r.f(index))
.finish(); .finish();
} }
``` ```
@ -37,7 +37,7 @@ fn index(req: HttpRequest) -> HttpResponse {
fn main() { fn main() {
Application::default("/app") Application::default("/app")
.route("/prefix", |r| r.f(index)) .resource("/prefix", |r| r.f(index))
.finish(); .finish();
} }
``` ```
@ -126,7 +126,7 @@ fn index(req: HttpRequest) -> Result<String> {
fn main() { fn main() {
Application::default("/") Application::default("/")
.resource(r"/a/{v1}/{v2}/", |r| r.route().f(index)) .resource(r"/a/{v1}/{v2}/", |r| r.f(index))
.finish(); .finish();
} }
``` ```

View File

@ -210,41 +210,6 @@ impl<S> ApplicationBuilder<S> where S: 'static {
self 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<F, P: Into<String>>(&mut self, path: P, f: F) -> &mut Self
where P: Into<String>,
F: FnOnce(&mut Route<S>) + '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 /// Register a middleware
pub fn middleware<T>(&mut self, mw: T) -> &mut Self pub fn middleware<T>(&mut self, mw: T) -> &mut Self
where T: Middleware + 'static where T: Middleware + 'static

View File

@ -198,7 +198,7 @@ impl FromRequest for FilesystemElement {
/// ///
/// fn main() { /// fn main() {
/// let app = actix_web::Application::default("/") /// 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(); /// .finish();
/// } /// }
/// ``` /// ```

View File

@ -3,7 +3,7 @@ use std::marker::PhantomData;
use http::Method; use http::Method;
use route::Route; use route::Route;
use handler::{Reply, Handler, RouteHandler, WrapHandler}; use handler::{Reply, Handler, FromRequest, RouteHandler, WrapHandler};
use httpcodes::HTTPNotFound; use httpcodes::HTTPNotFound;
use httprequest::HttpRequest; use httprequest::HttpRequest;
@ -79,17 +79,48 @@ impl<S> Resource<S> where S: 'static {
/// .f(|r| HttpResponse::Ok())) /// .f(|r| HttpResponse::Ok()))
/// .finish(); /// .finish();
/// } /// }
/// ```
pub fn route(&mut self) -> &mut Route<S> { pub fn route(&mut self) -> &mut Route<S> {
self.routes.push(Route::default()); self.routes.push(Route::default());
self.routes.last_mut().unwrap() self.routes.last_mut().unwrap()
} }
/// Register a new route and add method check to route. /// 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<S> { pub fn method(&mut self, method: Method) -> &mut Route<S> {
self.routes.push(Route::default()); self.routes.push(Route::default());
self.routes.last_mut().unwrap().method(method) 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<H: Handler<S>>(&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<F, R>(&mut self, handler: F)
where F: Fn(HttpRequest<S>) -> 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. /// Default handler is used if no matched route found.
/// By default `HTTPNotFound` is used. /// By default `HTTPNotFound` is used.
pub fn default_handler<H>(&mut self, handler: H) where H: Handler<S> { pub fn default_handler<H>(&mut self, handler: H) where H: Handler<S> {