mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-27 17:22:57 +01:00
remove Applicaiton::route, resource is enough
This commit is contained in:
parent
04ded5ba68
commit
87c7441f7d
@ -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");
|
||||
|
@ -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");
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
```
|
||||
|
@ -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");
|
||||
|
@ -41,13 +41,13 @@ use tokio_core::net::TcpStream;
|
||||
fn main() {
|
||||
HttpServer::<TcpStream, SocketAddr, _>::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(),
|
||||
]);
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ fn index(req: HttpRequest) -> FutureResult<HttpResponse, Error> {
|
||||
|
||||
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();
|
||||
}
|
||||
```
|
||||
|
@ -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<String> {
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
.resource(r"/a/{v1}/{v2}/", |r| r.route().f(index))
|
||||
.resource(r"/a/{v1}/{v2}/", |r| r.f(index))
|
||||
.finish();
|
||||
}
|
||||
```
|
||||
|
@ -210,41 +210,6 @@ impl<S> ApplicationBuilder<S> 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<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
|
||||
pub fn middleware<T>(&mut self, mw: T) -> &mut Self
|
||||
where T: Middleware + 'static
|
||||
|
@ -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();
|
||||
/// }
|
||||
/// ```
|
||||
|
@ -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<S> Resource<S> where S: 'static {
|
||||
/// .f(|r| HttpResponse::Ok()))
|
||||
/// .finish();
|
||||
/// }
|
||||
/// ```
|
||||
pub fn route(&mut self) -> &mut Route<S> {
|
||||
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<S> {
|
||||
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<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.
|
||||
/// By default `HTTPNotFound` is used.
|
||||
pub fn default_handler<H>(&mut self, handler: H) where H: Handler<S> {
|
||||
|
Loading…
Reference in New Issue
Block a user