1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-29 19:40:34 +00:00

move db code to separate module

This commit is contained in:
Nikolay Kim 2017-12-20 17:43:43 -08:00
parent 3c5fd18e02
commit bf23aa5d4b
3 changed files with 58 additions and 48 deletions

View file

@ -30,13 +30,10 @@ before_script:
script:
- USE_SKEPTIC=1 cargo test --features=alpn
- |
if [[ "$TRAVIS_RUST_VERSION" == "1.20.0" ]]; then
if [[ "$TRAVIS_RUST_VERSION" == "beta" ]]; then
cd examples/diesel && cargo check && cd ../..
cd examples/multipart && cargo check && cd ../..
cd examples/json && cargo check && cd ../..
fi
- |
if [[ "$TRAVIS_RUST_VERSION" == "beta" ]]; then
cd examples/template_tera && cargo check && cd ../..
cd examples/tls && cargo check && cd ../..
cd examples/websocket-chat && cargo check && cd ../..

53
examples/diesel/src/db.rs Normal file
View file

@ -0,0 +1,53 @@
//! Db executor actor
use uuid;
use diesel;
use actix_web::*;
use actix::prelude::*;
use diesel::prelude::*;
use models;
use schema;
/// This is db executor actor. We are going to run 3 of them in parallele.
pub struct DbExecutor(pub SqliteConnection);
/// This is only message that this actor can handle, but it is easy to extend number of
/// messages.
pub struct CreateUser {
pub name: String,
}
impl ResponseType for CreateUser {
type Item = models::User;
type Error = Error;
}
impl Actor for DbExecutor {
type Context = SyncContext<Self>;
}
impl Handler<CreateUser> for DbExecutor {
fn handle(&mut self, msg: CreateUser, _: &mut Self::Context)
-> Response<Self, CreateUser>
{
use self::schema::users::dsl::*;
let uuid = format!("{}", uuid::Uuid::new_v4());
let new_user = models::NewUser {
id: &uuid,
name: &msg.name,
};
diesel::insert_into(users)
.values(&new_user)
.execute(&self.0)
.expect("Error inserting person");
let mut items = users
.filter(id.eq(&uuid))
.load::<models::User>(&self.0)
.expect("Error loading person");
Self::reply(items.pop().unwrap())
}
}

View file

@ -21,9 +21,13 @@ use actix::prelude::*;
use diesel::prelude::*;
use futures::future::Future;
mod db;
mod models;
mod schema;
use db::{CreateUser, DbExecutor};
/// State with DbExecutor address
struct State {
db: SyncAddress<DbExecutor>,
@ -44,50 +48,6 @@ fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>>
}))
}
/// This is db executor actor. We are going to run 3 of them in parallele.
struct DbExecutor(SqliteConnection);
/// This is only message that this actor can handle, but it is easy to extend number of
/// messages.
struct CreateUser {
name: String,
}
impl ResponseType for CreateUser {
type Item = models::User;
type Error = Error;
}
impl Actor for DbExecutor {
type Context = SyncContext<Self>;
}
impl Handler<CreateUser> for DbExecutor {
fn handle(&mut self, msg: CreateUser, _: &mut Self::Context)
-> Response<Self, CreateUser>
{
use self::schema::users::dsl::*;
let uuid = format!("{}", uuid::Uuid::new_v4());
let new_user = models::NewUser {
id: &uuid,
name: &msg.name,
};
diesel::insert_into(users)
.values(&new_user)
.execute(&self.0)
.expect("Error inserting person");
let mut items = users
.filter(id.eq(&uuid))
.load::<models::User>(&self.0)
.expect("Error loading person");
Self::reply(items.pop().unwrap())
}
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
let _ = env_logger::init();