-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
73 lines (66 loc) · 1.92 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate diesel;
extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate futures;
extern crate r2d2;
extern crate uuid;
extern crate bytes;
extern crate dotenv;
extern crate chrono;
extern crate r2d2_diesel;
use actix::prelude::*;
use actix_web::{
error, http, middleware, server, App, AsyncResponder, Error, HttpMessage,
HttpRequest, HttpResponse,
};
use bytes::BytesMut;
use futures::{Future, Stream};
use diesel::prelude::*;
//use diesel::r2d2::ConnectionManager;
use r2d2_diesel::ConnectionManager;
use r2d2::Pool;
pub type DBPool = Pool<ConnectionManager<MysqlConnection>>;
/// This is db executor actor. We are going to run 3 of them in parallel.
pub struct DbExecutor(pub DBPool);
impl Actor for DbExecutor {
type Context = SyncContext<Self>;
}
#[derive(Debug)]
pub struct CreateMember {
pub email: String,
pub name: String,
pub phone_number: Option<String>,
pub password: String,
pub gender: i8,
}
impl Message for CreateMember {
type Result = Result<(), Error>;
}
impl Handler<CreateMember> for DbExecutor {
type Result = Result<(), Error>;
fn handle(&mut self, msg: CreateMember, _: &mut Self::Context) -> Self::Result {
Ok(())
}
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
use std::env;
let sys = actix::System::new("diesel-example");
let _ = dotenv::dotenv();
let url = env::var("MYSQL_DATABASE_URL")
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run unit tests");
// Start 3 db executor actors
let manager = ConnectionManager::<MysqlConnection>::new(url);
let pool = r2d2::Pool::new(manager)
.expect("Failed to create pool.");
pool.get().unwrap();
let addr = SyncArbiter::start(3, move || DbExecutor(pool.clone()));
}