mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 01:51:23 +02:00
rename module
This commit is contained in:
@ -11,8 +11,8 @@ Typically middlewares involves in following actions:
|
||||
* Modify application state
|
||||
* Access external services (redis, logging, sessions)
|
||||
|
||||
Middlewares are registered for each application and get executed in same order as
|
||||
registraton order. In general, *middleware* is a type that implements
|
||||
Middlewares are registered for each application and get executed in same order as
|
||||
registraton order. In general, *middleware* is a type that implements
|
||||
[*Middleware trait*](../actix_web/middlewares/trait.Middleware.html). Each method
|
||||
in this trait has default implementation. Each method can return result immidietly
|
||||
or *future* object.
|
||||
@ -24,7 +24,7 @@ Here is example of simple middleware that adds request and response headers:
|
||||
# extern crate actix_web;
|
||||
use http::{header, HttpTryFrom};
|
||||
use actix_web::*;
|
||||
use actix_web::middlewares::{Middleware, Started, Response};
|
||||
use actix_web::middleware::{Middleware, Started, Response};
|
||||
|
||||
struct Headers; // <- Our middleware
|
||||
|
||||
@ -68,7 +68,7 @@ Logging middleware has to be registered for each application.
|
||||
|
||||
### Usage
|
||||
|
||||
Create `Logger` middlewares with the specified `format`.
|
||||
Create `Logger` middleware with the specified `format`.
|
||||
Default `Logger` could be created with `default` method, it uses the default format:
|
||||
|
||||
```ignore
|
||||
@ -77,7 +77,7 @@ Default `Logger` could be created with `default` method, it uses the default for
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
use actix_web::Application;
|
||||
use actix_web::middlewares::Logger;
|
||||
use actix_web::middleware::Logger;
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
@ -90,8 +90,8 @@ fn main() {
|
||||
Here is example of default logging format:
|
||||
|
||||
```
|
||||
INFO:actix_web::middlewares::logger: 127.0.0.1:59934 [02/Dec/2017:00:21:43 -0800] "GET / HTTP/1.1" 302 0 "-" "curl/7.54.0" 0.000397
|
||||
INFO:actix_web::middlewares::logger: 127.0.0.1:59947 [02/Dec/2017:00:22:40 -0800] "GET /index.html HTTP/1.1" 200 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:57.0) Gecko/20100101 Firefox/57.0" 0.000646
|
||||
INFO:actix_web::middleware::logger: 127.0.0.1:59934 [02/Dec/2017:00:21:43 -0800] "GET / HTTP/1.1" 302 0 "-" "curl/7.54.0" 0.000397
|
||||
INFO:actix_web::middleware::logger: 127.0.0.1:59947 [02/Dec/2017:00:22:40 -0800] "GET /index.html HTTP/1.1" 200 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:57.0) Gecko/20100101 Firefox/57.0" 0.000646
|
||||
```
|
||||
|
||||
### Format
|
||||
@ -134,7 +134,7 @@ use actix_web::*;
|
||||
fn main() {
|
||||
let app = Application::new()
|
||||
.middleware(
|
||||
middlewares::DefaultHeaders::build()
|
||||
middleware::DefaultHeaders::build()
|
||||
.header("X-Version", "0.2")
|
||||
.finish())
|
||||
.resource("/test", |r| {
|
||||
@ -148,14 +148,14 @@ fn main() {
|
||||
## User sessions
|
||||
|
||||
Actix provides general solution for session management.
|
||||
[*Session storage*](../actix_web/middlewares/struct.SessionStorage.html) middleare can be
|
||||
[*Session storage*](../actix_web/middleware/struct.SessionStorage.html) middleare can be
|
||||
use with different backend types to store session data in different backends.
|
||||
By default only cookie session backend is implemented. Other backend implementations
|
||||
could be added later.
|
||||
|
||||
[*Cookie session backend*](../actix_web/middlewares/struct.CookieSessionBackend.html)
|
||||
uses signed cookies as session storage. *Cookie session backend* creates sessions which
|
||||
are limited to storing fewer than 4000 bytes of data (as the payload must fit into a
|
||||
[*Cookie session backend*](../actix_web/middleware/struct.CookieSessionBackend.html)
|
||||
uses signed cookies as session storage. *Cookie session backend* creates sessions which
|
||||
are limited to storing fewer than 4000 bytes of data (as the payload must fit into a
|
||||
single cookie). Internal server error get generated if session contains more than 4000 bytes.
|
||||
|
||||
You need to pass a random value to the constructor of *CookieSessionBackend*.
|
||||
@ -163,19 +163,19 @@ This is private key for cookie session. When this value is changed, all session
|
||||
Note that whatever you write into your session is visible by the user (but not modifiable).
|
||||
|
||||
In general case, you cretate
|
||||
[*Session storage*](../actix_web/middlewares/struct.SessionStorage.html) middleware
|
||||
and initializes it with specific backend implementation, like *CookieSessionBackend*.
|
||||
[*Session storage*](../actix_web/middleware/struct.SessionStorage.html) middleware
|
||||
and initializes it with specific backend implementation, like *CookieSessionBackend*.
|
||||
To access session data
|
||||
[*HttpRequest::session()*](../actix_web/middlewares/trait.RequestSession.html#tymethod.session)
|
||||
[*HttpRequest::session()*](../actix_web/middleware/trait.RequestSession.html#tymethod.session)
|
||||
method has to be used. This method returns
|
||||
[*Session*](../actix_web/middlewares/struct.Session.html) object, which allows to get or set
|
||||
[*Session*](../actix_web/middleware/struct.Session.html) object, which allows to get or set
|
||||
session data.
|
||||
|
||||
```rust
|
||||
# extern crate actix;
|
||||
# extern crate actix_web;
|
||||
use actix_web::*;
|
||||
use actix_web::middlewares::{RequestSession, SessionStorage, CookieSessionBackend};
|
||||
use actix_web::middleware::{RequestSession, SessionStorage, CookieSessionBackend};
|
||||
|
||||
fn index(mut req: HttpRequest) -> Result<&'static str> {
|
||||
// access session data
|
||||
@ -193,11 +193,11 @@ fn main() {
|
||||
# let sys = actix::System::new("basic-example");
|
||||
HttpServer::new(
|
||||
|| Application::new()
|
||||
.middleware(SessionStorage::new( // <- create session middlewares
|
||||
.middleware(SessionStorage::new( // <- create session middleware
|
||||
CookieSessionBackend::build(&[0; 32]) // <- create cookie session backend
|
||||
.secure(false)
|
||||
.finish()
|
||||
)))
|
||||
)))
|
||||
.bind("127.0.0.1:59880").unwrap()
|
||||
.start();
|
||||
# actix::Arbiter::system().send(actix::msgs::SystemExit(0));
|
||||
|
@ -62,16 +62,16 @@ All `/app1` requests route to first application, `/app2` to second and then all
|
||||
Application state is shared with all routes and resources within same application.
|
||||
State could be accessed with `HttpRequest::state()` method as a read-only item
|
||||
but interior mutability pattern with `RefCell` could be used to archive state mutability.
|
||||
State could be accessed with `HttpContext::state()` in case of http actor.
|
||||
State could be accessed with `HttpContext::state()` in case of http actor.
|
||||
State also available to route matching predicates and middlewares.
|
||||
|
||||
Let's write simple application that uses shared state. We are going to store requests count
|
||||
in the state:
|
||||
|
||||
in the state:
|
||||
|
||||
```rust
|
||||
# extern crate actix;
|
||||
# extern crate actix_web;
|
||||
#
|
||||
#
|
||||
use actix_web::*;
|
||||
use std::cell::Cell;
|
||||
|
||||
|
Reference in New Issue
Block a user