|
1 | 1 | // <actor> |
2 | | -// use actix::prelude::*; |
| 2 | +use actix::prelude::*; |
3 | 3 |
|
4 | | -// struct DbExecutor(SqliteConnection); |
| 4 | +struct DbExecutor(SqliteConnection); |
5 | 5 |
|
6 | | -// impl Actor for DbExecutor { |
7 | | -// type Context = SyncContext<Self>; |
8 | | -// } |
| 6 | +impl Actor for DbExecutor { |
| 7 | + type Context = SyncContext<Self>; |
| 8 | +} |
9 | 9 | // </actor> |
10 | 10 |
|
11 | 11 | // <message> |
12 | | -// struct CreateUser { |
13 | | -// name: String, |
14 | | -// } |
| 12 | +struct CreateUser { |
| 13 | + name: String, |
| 14 | +} |
15 | 15 |
|
16 | | -// impl Message for CreateUser { |
17 | | -// type Result = Result<User, Error>; |
18 | | -// } |
| 16 | +impl Message for CreateUser { |
| 17 | + type Result = Result<User, Error>; |
| 18 | +} |
19 | 19 | // </message> |
20 | 20 |
|
21 | 21 | // <handler> |
22 | | -// impl Handler<CreateUser> for DbExecutor { |
23 | | -// type Result = Result<User, Error>; |
24 | | - |
25 | | -// fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Self::Result { |
26 | | -// use self::schema::users::dsl::*; |
27 | | - |
28 | | -// // Create insertion model |
29 | | -// let uuid = format!("{}", uuid::Uuid::new_v4()); |
30 | | -// let new_user = models::NewUser { |
31 | | -// id: &uuid, |
32 | | -// name: &msg.name, |
33 | | -// }; |
34 | | - |
35 | | -// // normal diesel operations |
36 | | -// diesel::insert_into(users) |
37 | | -// .values(&new_user) |
38 | | -// .execute(&self.0) |
39 | | -// .expect("Error inserting person"); |
40 | | - |
41 | | -// let mut items = users |
42 | | -// .filter(id.eq(&uuid)) |
43 | | -// .load::<models::User>(&self.0) |
44 | | -// .expect("Error loading person"); |
45 | | - |
46 | | -// Ok(items.pop().unwrap()) |
47 | | -// } |
48 | | -// } |
| 22 | +impl Handler<CreateUser> for DbExecutor { |
| 23 | + type Result = Result<User, Error>; |
| 24 | + |
| 25 | + fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Self::Result { |
| 26 | + use self::schema::users::dsl::*; |
| 27 | + |
| 28 | + // Create insertion model |
| 29 | + let uuid = format!("{}", uuid::Uuid::new_v4()); |
| 30 | + let new_user = models::NewUser { |
| 31 | + id: &uuid, |
| 32 | + name: &msg.name, |
| 33 | + }; |
| 34 | + |
| 35 | + // normal diesel operations |
| 36 | + diesel::insert_into(users) |
| 37 | + .values(&new_user) |
| 38 | + .execute(&self.0) |
| 39 | + .expect("Error inserting person"); |
| 40 | + |
| 41 | + let mut items = users |
| 42 | + .filter(id.eq(&uuid)) |
| 43 | + .load::<models::User>(&self.0) |
| 44 | + .expect("Error loading person"); |
| 45 | + |
| 46 | + Ok(items.pop().unwrap()) |
| 47 | + } |
| 48 | +} |
49 | 49 | // </handler> |
50 | 50 |
|
51 | 51 | // <main> |
52 | | -// /// This is state where we will store *DbExecutor* address. |
53 | | -// struct State { |
54 | | -// db: Addr<DbExecutor>, |
55 | | -// } |
56 | | - |
57 | | -// fn main() { |
58 | | -// let sys = actix::System::new("diesel-example"); |
59 | | - |
60 | | -// // Start 3 parallel db executors |
61 | | -// let addr = SyncArbiter::start(3, || { |
62 | | -// DbExecutor(SqliteConnection::establish("test.db").unwrap()) |
63 | | -// }); |
64 | | - |
65 | | -// // Start http server |
66 | | -// HttpServer::new(move || { |
67 | | -// App::with_state(State { db: addr.clone() }) |
68 | | -// .resource("/{name}", |r| r.method(Method::GET).a(index)) |
69 | | -// }) |
70 | | -// .bind("127.0.0.1:8080") |
71 | | -// .unwrap() |
72 | | -// .start() |
73 | | -// .unwrap(); |
74 | | - |
75 | | -// println!("Started http server: 127.0.0.1:8080"); |
76 | | -// let _ = sys.run(); |
77 | | -// } |
| 52 | +/// This is state where we will store *DbExecutor* address. |
| 53 | +struct State { |
| 54 | + db: Addr<DbExecutor>, |
| 55 | +} |
| 56 | + |
| 57 | +fn main() { |
| 58 | + let sys = actix::System::new("diesel-example"); |
| 59 | + |
| 60 | + // Start 3 parallel db executors |
| 61 | + let addr = SyncArbiter::start(3, || { |
| 62 | + DbExecutor(SqliteConnection::establish("test.db").unwrap()) |
| 63 | + }); |
| 64 | + |
| 65 | + // Start http server |
| 66 | + HttpServer::new(move || { |
| 67 | + App::with_state(State { db: addr.clone() }) |
| 68 | + .resource("/{name}", |r| r.method(Method::GET).a(index)) |
| 69 | + }) |
| 70 | + .bind("127.0.0.1:8080") |
| 71 | + .unwrap() |
| 72 | + .start() |
| 73 | + .unwrap(); |
| 74 | + |
| 75 | + println!("Started http server: 127.0.0.1:8080"); |
| 76 | + let _ = sys.run(); |
| 77 | +} |
78 | 78 | // </main> |
79 | 79 |
|
80 | 80 | // <index> |
81 | | -// /// Async handler |
82 | | -// fn index(req: &HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Error>> { |
83 | | -// let name = &req.match_info()["name"]; |
84 | | - |
85 | | -// // Send message to `DbExecutor` actor |
86 | | -// req.state() |
87 | | -// .db |
88 | | -// .send(CreateUser { |
89 | | -// name: name.to_owned(), |
90 | | -// }) |
91 | | -// .from_err() |
92 | | -// .and_then(|res| match res { |
93 | | -// Ok(user) => Ok(HttpResponse::Ok().json(user)), |
94 | | -// Err(_) => Ok(HttpResponse::InternalServerError().into()), |
95 | | -// }) |
96 | | -// .responder() |
97 | | -// } |
| 81 | +/// Async handler |
| 82 | +fn index(req: &HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Error>> { |
| 83 | + let name = &req.match_info()["name"]; |
| 84 | + |
| 85 | + // Send message to `DbExecutor` actor |
| 86 | + req.state() |
| 87 | + .db |
| 88 | + .send(CreateUser { |
| 89 | + name: name.to_owned(), |
| 90 | + }) |
| 91 | + .from_err() |
| 92 | + .and_then(|res| match res { |
| 93 | + Ok(user) => Ok(HttpResponse::Ok().json(user)), |
| 94 | + Err(_) => Ok(HttpResponse::InternalServerError().into()), |
| 95 | + }) |
| 96 | + .responder() |
| 97 | +} |
98 | 98 | // </index> |
0 commit comments