mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-28 01:52:57 +01:00
fix typos
This commit is contained in:
parent
56fd088163
commit
669975df75
@ -3,13 +3,13 @@
|
||||
## Diesel
|
||||
|
||||
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.
|
||||
Multipl sync actors could be started, in this case all of this actor
|
||||
process messages from same queu (sync actors actually work mpmc mode).
|
||||
But it possible to use `actix` synchronous actor system as a db interface api.
|
||||
Multiple sync actors could be run in parallel, in this case all of this actors
|
||||
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.
|
||||
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
|
||||
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
|
||||
struct CreateUser {
|
||||
@ -35,10 +35,11 @@ impl ResponseType for CreateUser {
|
||||
```
|
||||
|
||||
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
|
||||
impl Handler<CreateUser> for DbExecutor {
|
||||
|
||||
fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Response<Self, CreateUser>
|
||||
{
|
||||
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.
|
||||
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.
|
||||
|
||||
|
||||
```rust,ignore
|
||||
/// This is state where we sill store *DbExecutor* address.
|
||||
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
|
||||
used for async handler registration.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user