mirror of
https://github.com/actix/actix-website
synced 2025-01-22 16:15:56 +01:00
s/Actix-web/Actix Web
This commit is contained in:
parent
36b3f9a3d7
commit
12cbd2ebbf
@ -60,7 +60,7 @@ In the above example, the `show_users` route will have an effective route patter
|
|||||||
|
|
||||||
## Application guards and virtual hosting
|
## Application guards and virtual hosting
|
||||||
|
|
||||||
You can think of a guard as a simple function that accepts a _request_ object reference and returns _true_ or _false_. Formally, a guard is any object that implements the [`Guard`][guardtrait] trait. Actix-web provides several guards. You can check the [functions section][guardfuncs] of the API docs.
|
You can think of a guard as a simple function that accepts a _request_ object reference and returns _true_ or _false_. Formally, a guard is any object that implements the [`Guard`][guardtrait] trait. Actix Web provides several guards. You can check the [functions section][guardfuncs] of the API docs.
|
||||||
|
|
||||||
One of the provided guards is [`Header`][guardheader]. It can be used as a filter based on request header information.
|
One of the provided guards is [`Header`][guardheader]. It can be used as a filter based on request header information.
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ weight: 180
|
|||||||
|
|
||||||
# Errors
|
# Errors
|
||||||
|
|
||||||
Actix-web uses its own [`actix_web::error::Error`][actixerror] type and [`actix_web::error::ResponseError`][responseerror] trait for error handling from web handlers.
|
Actix Web uses its own [`actix_web::error::Error`][actixerror] type and [`actix_web::error::ResponseError`][responseerror] trait for error handling from web handlers.
|
||||||
|
|
||||||
If a handler returns an `Error` (referring to the [general Rust trait `std::error::Error`][stderror]) in a `Result` that also implements the `ResponseError` trait, actix-web will render that error as an HTTP response with its corresponding [`actix_web::http::StatusCode`][status_code]. An internal server error is generated by default:
|
If a handler returns an `Error` (referring to the [general Rust trait `std::error::Error`][stderror]) in a `Result` that also implements the `ResponseError` trait, actix-web will render that error as an HTTP response with its corresponding [`actix_web::http::StatusCode`][status_code]. An internal server error is generated by default:
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ impl<T: Responder, E: Into<Error>> Responder for Result<T, E>
|
|||||||
|
|
||||||
`Error` in the code above is actix-web's error definition, and any errors that implement `ResponseError` can be converted to one automatically.
|
`Error` in the code above is actix-web's error definition, and any errors that implement `ResponseError` can be converted to one automatically.
|
||||||
|
|
||||||
Actix-web provides `ResponseError` implementations for some common non-actix errors. For example, if a handler responds with an `io::Error`, that error is converted into an `HttpInternalServerError`:
|
Actix Web provides `ResponseError` implementations for some common non-actix errors. For example, if a handler responds with an `io::Error`, that error is converted into an `HttpInternalServerError`:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use std::io;
|
use std::io;
|
||||||
@ -52,7 +52,7 @@ Override `error_response()` to produce more useful results:
|
|||||||
|
|
||||||
# Error helpers
|
# Error helpers
|
||||||
|
|
||||||
Actix-web provides a set of error helper functions that are useful for generating specific HTTP error codes from other errors. Here we convert `MyError`, which doesn't implement the `ResponseError` trait, to a _400_ (bad request) using `map_err`:
|
Actix Web provides a set of error helper functions that are useful for generating specific HTTP error codes from other errors. Here we convert `MyError`, which doesn't implement the `ResponseError` trait, to a _400_ (bad request) using `map_err`:
|
||||||
|
|
||||||
{{< include-example example="errors" file="helpers.rs" section="helpers" >}}
|
{{< include-example example="errors" file="helpers.rs" section="helpers" >}}
|
||||||
|
|
||||||
|
@ -6,9 +6,9 @@ weight: 170
|
|||||||
|
|
||||||
# Type-safe information extraction
|
# Type-safe information extraction
|
||||||
|
|
||||||
Actix-web provides a facility for type-safe request information access called _extractors_ (i.e., `impl FromRequest`). By default, actix-web provides several extractor implementations.
|
Actix Web provides a facility for type-safe request information access called _extractors_ (i.e., `impl FromRequest`). By default, actix-web provides several extractor implementations.
|
||||||
|
|
||||||
An extractor can be accessed as an argument to a handler function. Actix-web supports up to 12 extractors per handler function. Argument position does not matter.
|
An extractor can be accessed as an argument to a handler function. Actix Web supports up to 12 extractors per handler function. Argument position does not matter.
|
||||||
|
|
||||||
{{< include-example example="extractors" file="main.rs" section="option-one" >}}
|
{{< include-example example="extractors" file="main.rs" section="option-one" >}}
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ At the moment, only url-encoded forms are supported. The url-encoded body could
|
|||||||
|
|
||||||
# Other
|
# Other
|
||||||
|
|
||||||
Actix-web also provides several other extractors:
|
Actix Web also provides several other extractors:
|
||||||
|
|
||||||
- [_Data_][datastruct] - If you need access to an application state.
|
- [_Data_][datastruct] - If you need access to an application state.
|
||||||
- _HttpRequest_ - _HttpRequest_ itself is an extractor which returns self, in case you need access to the request.
|
- _HttpRequest_ - _HttpRequest_ itself is an extractor which returns self, in case you need access to the request.
|
||||||
|
@ -6,7 +6,7 @@ weight: 220
|
|||||||
|
|
||||||
# Middleware
|
# Middleware
|
||||||
|
|
||||||
Actix-web's middleware system allows us to add additional behavior to request/response processing. Middleware can hook into an incoming request process, enabling us to modify requests as well as halt request processing to return a response early.
|
Actix Web's middleware system allows us to add additional behavior to request/response processing. Middleware can hook into an incoming request process, enabling us to modify requests as well as halt request processing to return a response early.
|
||||||
|
|
||||||
Middleware can also hook into response processing.
|
Middleware can also hook into response processing.
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ Alternatively, for simple use cases, you can use [_wrap_fn_][wrap_fn] to create
|
|||||||
|
|
||||||
{{< include-example example="middleware" file="wrap_fn.rs" section="wrap-fn" >}}
|
{{< include-example example="middleware" file="wrap_fn.rs" section="wrap-fn" >}}
|
||||||
|
|
||||||
> Actix-web provides several useful middleware, such as _logging_, _user sessions_, _compress_, etc.
|
> Actix Web provides several useful middleware, such as _logging_, _user sessions_, _compress_, etc.
|
||||||
|
|
||||||
**Warning: if you use `wrap()` or `wrap_fn()` multiple times, the last occurrence will be executed first.**
|
**Warning: if you use `wrap()` or `wrap_fn()` multiple times, the last occurrence will be executed first.**
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ To set default response headers, the `DefaultHeaders` middleware can be used. Th
|
|||||||
|
|
||||||
## User sessions
|
## User sessions
|
||||||
|
|
||||||
Actix-web provides a general solution for session management. The [**actix-session**][actixsession] middleware can use multiple backend types to store session data.
|
Actix Web provides a general solution for session management. The [**actix-session**][actixsession] middleware can use multiple backend types to store session data.
|
||||||
|
|
||||||
> By default, only cookie session backend is implemented. Other backend implementations can be added.
|
> By default, only cookie session backend is implemented. Other backend implementations can be added.
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ In the following example, we will deserialize a _MyObj_ struct. We need to load
|
|||||||
|
|
||||||
# Content Encoding
|
# Content Encoding
|
||||||
|
|
||||||
Actix-web automatically _decompresses_ payloads. The following codecs are supported:
|
Actix Web automatically _decompresses_ payloads. The following codecs are supported:
|
||||||
|
|
||||||
- Brotli
|
- Brotli
|
||||||
- Gzip
|
- Gzip
|
||||||
@ -55,13 +55,13 @@ Actix automatically decodes _chunked_ encoding. The [`web::Payload`][payloadextr
|
|||||||
|
|
||||||
# Multipart body
|
# Multipart body
|
||||||
|
|
||||||
Actix-web provides multipart stream support with an external crate, [`actix-multipart`][multipartcrate].
|
Actix Web provides multipart stream support with an external crate, [`actix-multipart`][multipartcrate].
|
||||||
|
|
||||||
> A full example is available in the [examples directory][multipartexample].
|
> A full example is available in the [examples directory][multipartexample].
|
||||||
|
|
||||||
# Urlencoded body
|
# Urlencoded body
|
||||||
|
|
||||||
Actix-web provides support for _application/x-www-form-urlencoded_ encoded bodies with the [`web::Form`][formencoded] extractor which resolves to the deserialized instance. The type of the instance must implement the `Deserialize` trait from _serde_.
|
Actix Web provides support for _application/x-www-form-urlencoded_ encoded bodies with the [`web::Form`][formencoded] extractor which resolves to the deserialized instance. The type of the instance must implement the `Deserialize` trait from _serde_.
|
||||||
|
|
||||||
The _UrlEncoded_ future can resolve into an error in several cases:
|
The _UrlEncoded_ future can resolve into an error in several cases:
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ Using the `Json` type this way instead of calling the `.json` method on a `HttpR
|
|||||||
|
|
||||||
# Content encoding
|
# Content encoding
|
||||||
|
|
||||||
Actix-web can automatically _compress_ payloads with the [_Compress middleware_][compressmidddleware]. The following codecs are supported:
|
Actix Web can automatically _compress_ payloads with the [_Compress middleware_][compressmidddleware]. The following codecs are supported:
|
||||||
|
|
||||||
- Brotli
|
- Brotli
|
||||||
- Gzip
|
- Gzip
|
||||||
|
@ -6,7 +6,7 @@ weight: 215
|
|||||||
|
|
||||||
# Testing
|
# Testing
|
||||||
|
|
||||||
Every application should be well tested. Actix-web provides tools to perform unit and integration tests.
|
Every application should be well tested. Actix Web provides tools to perform unit and integration tests.
|
||||||
|
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ For unit testing, actix-web provides a request builder type. [_TestRequest_][tes
|
|||||||
|
|
||||||
# Integration tests
|
# Integration tests
|
||||||
|
|
||||||
There are a few methods for testing your application. Actix-web can be used to run the application with specific handlers in a real HTTP server.
|
There are a few methods for testing your application. Actix Web can be used to run the application with specific handlers in a real HTTP server.
|
||||||
|
|
||||||
`TestRequest::get()`, `TestRequest::post()` and other methods can be used to send requests to the test server.
|
`TestRequest::get()`, `TestRequest::post()` and other methods can be used to send requests to the test server.
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ menu: docs_protocols
|
|||||||
weight: 240
|
weight: 240
|
||||||
---
|
---
|
||||||
|
|
||||||
Actix-web supports WebSockets with the `actix-web-actors` crate. It is possible to convert a request's `Payload` to a stream of [_ws::Message_][message] with a [_web::Payload_][payload] and then use stream combinators to handle actual messages, but it is simpler to handle websocket communications with an http actor.
|
Actix Web supports WebSockets with the `actix-web-actors` crate. It is possible to convert a request's `Payload` to a stream of [_ws::Message_][message] with a [_web::Payload_][payload] and then use stream combinators to handle actual messages, but it is simpler to handle websocket communications with an http actor.
|
||||||
|
|
||||||
The following is an example of a simple websocket echo server:
|
The following is an example of a simple websocket echo server:
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(web::Data::new(AppState {
|
.app_data(web::Data::new(AppState {
|
||||||
app_name: String::from("Actix-web"),
|
app_name: String::from("Actix Web"),
|
||||||
}))
|
}))
|
||||||
.service(index)
|
.service(index)
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user