mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
fix typos
This commit is contained in:
parent
56fd088163
commit
669975df75
@ -3,13 +3,13 @@
|
|||||||
## Diesel
|
## Diesel
|
||||||
|
|
||||||
At the moment of 1.0 release Diesel does not support asynchronous operations.
|
At the moment of 1.0 release Diesel does not support asynchronous operations.
|
||||||
But it possible to use `actix` synchronous actor as an db interface api.
|
But it possible to use `actix` synchronous actor system as a db interface api.
|
||||||
Multipl sync actors could be started, in this case all of this actor
|
Multiple sync actors could be run in parallel, in this case all of this actors
|
||||||
process messages from same queu (sync actors actually work mpmc mode).
|
process messages from the same queue (sync actors actually work in mpmc mode).
|
||||||
|
|
||||||
Let's create simple db api that can insert new user row into sqlite table.
|
Let's create simple db api that can insert new user row into sqlite table.
|
||||||
We need to define sync actor and connection that this actor will use. Same approach
|
We need to define sync actor and connection that this actor will use. Same approach
|
||||||
could used for other databases:
|
could used for other databases.
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
use actix::prelude::*;*
|
use actix::prelude::*;*
|
||||||
@ -21,7 +21,7 @@ impl Actor for DbExecutor {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This is definition of our actor. Now we need to define *create user* message.
|
This is definition of our actor. Now we need to define *create user* message and response.
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
struct CreateUser {
|
struct CreateUser {
|
||||||
@ -35,10 +35,11 @@ impl ResponseType for CreateUser {
|
|||||||
```
|
```
|
||||||
|
|
||||||
We can send `CreateUser` message to `DbExecutor` actor, and as result we can get
|
We can send `CreateUser` message to `DbExecutor` actor, and as result we can get
|
||||||
`User` model. Now we need to define actual handler for this message.
|
`User` model. Now we need to define actual handler implementation for this message.
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
impl Handler<CreateUser> for DbExecutor {
|
impl Handler<CreateUser> for DbExecutor {
|
||||||
|
|
||||||
fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Response<Self, CreateUser>
|
fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Response<Self, CreateUser>
|
||||||
{
|
{
|
||||||
use self::schema::users::dsl::*;
|
use self::schema::users::dsl::*;
|
||||||
@ -67,10 +68,9 @@ impl Handler<CreateUser> for DbExecutor {
|
|||||||
```
|
```
|
||||||
|
|
||||||
That is it. Now we can use *DbExecutor* actor from any http handler or middleware.
|
That is it. Now we can use *DbExecutor* actor from any http handler or middleware.
|
||||||
All we need is to start *DbExecutor* actors and store address in state where http endpoint
|
All we need is to start *DbExecutor* actors and store address in state where http handler
|
||||||
can access it.
|
can access it.
|
||||||
|
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
/// This is state where we sill store *DbExecutor* address.
|
/// This is state where we sill store *DbExecutor* address.
|
||||||
struct State {
|
struct State {
|
||||||
@ -97,7 +97,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
And finally we can use this handler function. We get message response
|
And finally we can use this state in handler function. We get message response
|
||||||
asynchronously, so handler needs to return future object, also `Route::a()` needs to be
|
asynchronously, so handler needs to return future object, also `Route::a()` needs to be
|
||||||
used for async handler registration.
|
used for async handler registration.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user