1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

update guide

This commit is contained in:
Nikolay Kim 2017-12-04 20:38:38 -08:00
parent fd6b243cd6
commit 3c9b6ea619
3 changed files with 37 additions and 8 deletions

View File

@ -11,7 +11,7 @@ Prefix handler:
```rust ```rust
# extern crate actix_web; # extern crate actix_web;
# use actix_web::*; # use actix_web::*;
#
fn index(req: HttpRequest) -> HttpResponse { fn index(req: HttpRequest) -> HttpResponse {
unimplemented!() unimplemented!()
} }
@ -30,7 +30,7 @@ Application prefix combines with handler prefix i.e
```rust ```rust
# extern crate actix_web; # extern crate actix_web;
# use actix_web::*; # use actix_web::*;
#
fn index(req: HttpRequest) -> HttpResponse { fn index(req: HttpRequest) -> HttpResponse {
unimplemented!() unimplemented!()
} }
@ -51,7 +51,7 @@ if no route could be matched default response `HTTPMethodNotAllowed` get resturn
```rust ```rust
# extern crate actix_web; # extern crate actix_web;
# use actix_web::*; # use actix_web::*;
#
fn main() { fn main() {
Application::default("/") Application::default("/")
.resource("/prefix", |r| { .resource("/prefix", |r| {
@ -102,7 +102,7 @@ You can also specify a custom regex in the form `{identifier:regex}`:
# fn index(req: HttpRequest) -> String { # fn index(req: HttpRequest) -> String {
# format!("Hello, {}", &req.match_info()["name"]) # format!("Hello, {}", &req.match_info()["name"])
# } # }
#
fn main() { fn main() {
Application::default("/") Application::default("/")
.resource(r"{name:\d+}", |r| r.method(Method::GET).f(index)) .resource(r"{name:\d+}", |r| r.method(Method::GET).f(index))
@ -115,7 +115,7 @@ implements `FromParam` trait. For example most of standard integer types
implements `FromParam` trait. i.e.: implements `FromParam` trait. i.e.:
```rust ```rust
extern crate actix_web; # extern crate actix_web;
use actix_web::*; use actix_web::*;
fn index(req: HttpRequest) -> Result<String> { fn index(req: HttpRequest) -> Result<String> {
@ -135,7 +135,13 @@ For this example for path '/a/1/2/', values v1 and v2 will resolve to "1" and "2
It is possible to match path tail with custom `.*` regex. It is possible to match path tail with custom `.*` regex.
```rust,ignore ```rust
# extern crate actix_web;
# use actix_web::*;
#
# fn index(req: HttpRequest) -> HttpResponse {
# unimplemented!()
# }
fn main() { fn main() {
Application::default("/") Application::default("/")
.resource(r"/test/{tail:.*}", |r| r.method(Method::GET).f(index)) .resource(r"/test/{tail:.*}", |r| r.method(Method::GET).f(index))

View File

@ -1,9 +1,31 @@
# HttpRequest & HttpResponse # HttpRequest & HttpResponse
## Response
Builder-like patter is used to construct an instance of `HttpResponse`.
`HttpResponse` provides several method that returns `HttpResponseBuilder` instance,
which is implements various convinience methods that helps build response.
Check [documentation](../actix_web/dev/struct.HttpResponseBuilder.html)
for type description. Methods `.body`, `.finish`, `.json` finalizes response creation,
if this methods get call for the same builder instance, builder will panic.
```rust
# extern crate actix_web;
use actix_web::*;
fn index(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_encoding(ContentEncoding::Br)
.content_type("plain/text")
.header("X-Hdr", "sample")
.body("data").unwrap()
}
# fn main() {}
```
## Content encoding ## Content encoding
Actix automatically *compress*/*decompress* payload. Actix automatically *compress*/*decompress* payload. Following codecs are supported:
Following codecs are supported:
* Brotli * Brotli
* Gzip * Gzip

View File

@ -373,6 +373,7 @@ impl HttpResponseBuilder {
self self
} }
/// Calls provided closure with builder reference if value is true.
pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Self pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Self
where F: Fn(&mut HttpResponseBuilder) + 'static where F: Fn(&mut HttpResponseBuilder) + 'static
{ {