|
| 1 | +use actix::prelude::*; |
1 | 2 | use actix_files::{Files, NamedFile};
|
2 |
| -use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder}; |
3 |
| -use std::sync::{Arc, Mutex}; |
4 |
| -use std::{thread, time}; |
5 |
| - |
6 |
| -// @TODO -- Once Actix 1.0 is stable and documentation is updated -- |
7 |
| -// @TODO: rewrite to actors (State and thread::sleep) |
8 |
| -// @TODO: add Api handlers into one scope + better non-existent API handling |
9 |
| -// @TODO: cannot use value 3000 as a 'delay' - a weird actix bug? |
| 3 | +use actix_web::{get, post, web, App, HttpServer}; |
| 4 | +use std::time; |
| 5 | +use tokio_timer; |
10 | 6 |
|
11 | 7 | use shared;
|
12 | 8 |
|
13 |
| -type State = Arc<Mutex<StateData>>; |
| 9 | +mod count_actor; |
| 10 | +use count_actor::{CountActor, MsgIncrement}; |
14 | 11 |
|
15 |
| -#[derive(Default)] |
16 |
| -struct StateData { |
17 |
| - message_ordinal_number: u32, |
18 |
| -} |
| 12 | +// ---- Apis ("/api/*") ---- |
19 | 13 |
|
20 |
| -#[post("/api/send-message")] |
| 14 | +#[post("send-message")] |
21 | 15 | fn send_message(
|
22 | 16 | state: web::Data<State>,
|
23 | 17 | request_data: web::Json<shared::SendMessageRequestBody>,
|
24 |
| -) -> impl Responder { |
25 |
| - state.lock().unwrap().message_ordinal_number += 1; |
26 |
| - web::Json(shared::SendMessageResponseBody { |
27 |
| - ordinal_number: state.lock().unwrap().message_ordinal_number, |
28 |
| - text: request_data.text.clone(), |
29 |
| - }) |
| 18 | +) -> impl Future<Item = web::Json<shared::SendMessageResponseBody>, Error = actix::MailboxError> { |
| 19 | + let text = request_data.text.clone(); |
| 20 | + state |
| 21 | + .count_actor |
| 22 | + .send(MsgIncrement) |
| 23 | + .and_then(move |ordinal_number| { |
| 24 | + Ok(web::Json(shared::SendMessageResponseBody { |
| 25 | + ordinal_number, |
| 26 | + text, |
| 27 | + })) |
| 28 | + }) |
30 | 29 | }
|
31 | 30 |
|
32 |
| -#[get("/api/delayed-response/{delay}")] |
33 |
| -fn delayed_response(delay: web::Path<(u64)>) -> impl Responder { |
34 |
| - thread::sleep(time::Duration::from_millis(*delay)); |
35 |
| - format!("Delay was set to {}ms.", delay) |
| 31 | +#[get("delayed-response/{delay}")] |
| 32 | +fn delayed_response( |
| 33 | + delay: web::Path<(u64)>, |
| 34 | +) -> impl Future<Item = String, Error = tokio_timer::Error> { |
| 35 | + tokio_timer::sleep(time::Duration::from_millis(*delay)) |
| 36 | + .and_then(move |()| Ok(format!("Delay was set to {}ms.", delay))) |
36 | 37 | }
|
37 | 38 |
|
38 |
| -#[get("/api/*")] |
39 |
| -fn non_existent_api() -> impl Responder { |
40 |
| - HttpResponse::NotFound() |
41 |
| -} |
42 |
| - |
43 |
| -#[get("*")] |
44 |
| -fn index() -> impl Responder { |
45 |
| - NamedFile::open("./client/index.html") |
| 39 | +struct State { |
| 40 | + count_actor: Addr<CountActor>, |
46 | 41 | }
|
47 | 42 |
|
48 | 43 | fn main() -> std::io::Result<()> {
|
49 |
| - let state = Arc::new(Mutex::new(StateData::default())); |
| 44 | + let system = System::new("server-integration-example"); |
| 45 | + |
| 46 | + let count_actor_addr = CountActor(0).start(); |
50 | 47 |
|
51 | 48 | HttpServer::new(move || {
|
52 | 49 | App::new()
|
53 |
| - .data(state.clone()) |
54 |
| - .service(send_message) |
55 |
| - .service(delayed_response) |
56 |
| - .service(non_existent_api) |
| 50 | + .data(State { |
| 51 | + count_actor: count_actor_addr.clone(), |
| 52 | + }) |
| 53 | + .service( |
| 54 | + web::scope("/api/") |
| 55 | + .service(send_message) |
| 56 | + .service(delayed_response), |
| 57 | + ) |
57 | 58 | .service(Files::new("/public", "./client/public"))
|
58 | 59 | .service(Files::new("/pkg", "./client/pkg"))
|
59 |
| - .service(index) |
| 60 | + .default_service(web::get().to(|| NamedFile::open("./client/index.html"))) |
60 | 61 | })
|
61 | 62 | .bind("127.0.0.1:8000")?
|
62 |
| - .run() |
| 63 | + .run()?; |
| 64 | + |
| 65 | + system.run() |
63 | 66 | }
|
0 commit comments