mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-30 18:44:35 +01:00
update guide
This commit is contained in:
parent
c47e2ccfee
commit
7fc7d6e17a
@ -83,6 +83,27 @@ if request contains `Content-Type` header and value of this header is *text/plai
|
|||||||
and path equals to `/test`. Resource calls handle of the first matches route.
|
and path equals to `/test`. Resource calls handle of the first matches route.
|
||||||
If resource can not match any route "NOT FOUND" response get returned.
|
If resource can not match any route "NOT FOUND" response get returned.
|
||||||
|
|
||||||
|
[*Resource::route()*](../actix_web/struct.Resource.html#method.route) method returns
|
||||||
|
[*Route*](../actix_web/struct.Route.html) object. Route can be configured with
|
||||||
|
builder-like pattern. Following configuration methods are available:
|
||||||
|
|
||||||
|
* [*Route::p()*](../actix_web/struct.Route.html#method.p) method registers new predicate,
|
||||||
|
any number of predicates could be registered for each route.
|
||||||
|
|
||||||
|
* [*Route::f()*](../actix_web/struct.Route.html#method.f) method registers handler function
|
||||||
|
for this route. Only one handler could be registered. Usually handler registeration
|
||||||
|
is the last config operation. Handler fanction could be function or closure and has type
|
||||||
|
`Fn(HttpRequest<S>) -> R + 'static`
|
||||||
|
|
||||||
|
* [*Route::h()*](../actix_web/struct.Route.html#method.h) method registers handler object
|
||||||
|
that implements `Handler` trait. This is similar to `f()` method, only one handler could
|
||||||
|
be registered. Handler registeration is the last config operation.
|
||||||
|
|
||||||
|
* [*Route::a()*](../actix_web/struct.Route.html#method.a) method registers asynchandler
|
||||||
|
function for this route. Only one handler could be registered. Handler registeration
|
||||||
|
is the last config operation. Handler fanction could be function or closure and has type
|
||||||
|
`Fn(HttpRequest<S>) -> Future<Item = HttpResponse, Error = Error> + 'static`
|
||||||
|
|
||||||
## Route matching
|
## Route matching
|
||||||
|
|
||||||
The main purpose of route configuration is to match (or not match) the request's `path`
|
The main purpose of route configuration is to match (or not match) the request's `path`
|
||||||
@ -323,7 +344,7 @@ fn index(req: HttpRequest) -> HttpResponse {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let app = Application::new()
|
let app = Application::new()
|
||||||
.resource("/test/{one}/{two}/{three}", |r| {
|
.resource("/test/{a}/{b}/{c}", |r| {
|
||||||
r.name("foo"); // <- set resource name, then it could be used in `url_for`
|
r.name("foo"); // <- set resource name, then it could be used in `url_for`
|
||||||
r.method(Method::GET).f(|_| httpcodes::HTTPOk);
|
r.method(Method::GET).f(|_| httpcodes::HTTPOk);
|
||||||
})
|
})
|
||||||
@ -331,7 +352,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This would return something like the string *http://example.com/1/2/3* (at least if
|
This would return something like the string *http://example.com/test/1/2/3* (at least if
|
||||||
the current protocol and hostname implied http://example.com).
|
the current protocol and hostname implied http://example.com).
|
||||||
`url_for()` method return [*Url object*](https://docs.rs/url/1.6.0/url/struct.Url.html) so you
|
`url_for()` method return [*Url object*](https://docs.rs/url/1.6.0/url/struct.Url.html) so you
|
||||||
can modify this url (add query parameters, anchor, etc).
|
can modify this url (add query parameters, anchor, etc).
|
||||||
@ -462,8 +483,7 @@ and returns *true* or *false*. Formally predicate is any object that implements
|
|||||||
several predicates, you can check [functions section](../actix_web/pred/index.html#functions)
|
several predicates, you can check [functions section](../actix_web/pred/index.html#functions)
|
||||||
of api docs.
|
of api docs.
|
||||||
|
|
||||||
Here is simple predicates that check that request contains specific *header* and predicate
|
Here is simple predicates that check that request contains specific *header*:
|
||||||
usage:
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# extern crate actix_web;
|
# extern crate actix_web;
|
||||||
@ -538,9 +558,9 @@ predicates match. i.e:
|
|||||||
## Changing the default Not Found response
|
## Changing the default Not Found response
|
||||||
|
|
||||||
If path pattern can not be found in routing table or resource can not find matching
|
If path pattern can not be found in routing table or resource can not find matching
|
||||||
route default resource is used. Default response is *NOT FOUND* response.
|
route, default resource is used. Default response is *NOT FOUND* response.
|
||||||
To override *NOT FOUND* resource use `Application::default_resource()` method.
|
It is possible to override *NOT FOUND* response with `Application::default_resource()` method.
|
||||||
This method accepts *configuration function* same as normal resource registration
|
This method accepts *configuration function* same as normal resource configuration
|
||||||
with `Application::resource()` method.
|
with `Application::resource()` method.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@ -555,6 +575,6 @@ fn main() {
|
|||||||
r.method(Method::GET).f(|req| HTTPNotFound);
|
r.method(Method::GET).f(|req| HTTPNotFound);
|
||||||
r.route().p(pred::Not(pred::Get())).f(|req| HTTPMethodNotAllowed);
|
r.route().p(pred::Not(pred::Get())).f(|req| HTTPMethodNotAllowed);
|
||||||
})
|
})
|
||||||
.finish();
|
# .finish();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -92,7 +92,7 @@ impl<S: 'static> Resource<S> {
|
|||||||
/// This is shortcut for:
|
/// This is shortcut for:
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
/// Resource::resource("/", |r| r.route().method(Method::GET).f(index)
|
/// Resource::resource("/", |r| r.route().p(pred::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());
|
||||||
|
16
src/route.rs
16
src/route.rs
@ -43,6 +43,22 @@ impl<S: 'static> Route<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add match predicate to route.
|
/// Add match predicate to route.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # extern crate actix_web;
|
||||||
|
/// # use actix_web::*;
|
||||||
|
/// # use actix_web::httpcodes::*;
|
||||||
|
/// # fn main() {
|
||||||
|
/// Application::new()
|
||||||
|
/// .resource("/path", |r|
|
||||||
|
/// r.route()
|
||||||
|
/// .p(pred::Get())
|
||||||
|
/// .p(pred::Header("content-type", "text/plain"))
|
||||||
|
/// .f(|req| HTTPOk)
|
||||||
|
/// )
|
||||||
|
/// # .finish();
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub fn p(&mut self, p: Box<Predicate<S>>) -> &mut Self {
|
pub fn p(&mut self, p: Box<Predicate<S>>) -> &mut Self {
|
||||||
self.preds.push(p);
|
self.preds.push(p);
|
||||||
self
|
self
|
||||||
|
Loading…
Reference in New Issue
Block a user