mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 09:59:21 +02:00
remove Applicaiton::route, resource is enough
This commit is contained in:
@ -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> {
|
||||
|
Reference in New Issue
Block a user