Skip to content

Commit 787fb5b

Browse files
MartinKavikDavid-OConnor
authored andcommitted
actix-actors
1 parent 4fbed7a commit 787fb5b

File tree

4 files changed

+130
-47
lines changed

4 files changed

+130
-47
lines changed

examples/server_integration/Cargo.lock

+58-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/server_integration/server/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ authors = ["Your Name <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8+
actix = "0.8.3"
89
actix-web = "1.0.0"
910
actix-files = "0.1.1"
11+
tokio-timer = "0.2.11"
1012

1113
serde_json = "^1.0.39"
1214

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use actix::prelude::*;
2+
3+
// ---- Actor ----
4+
5+
pub struct CountActor(pub u32);
6+
7+
impl Actor for CountActor {
8+
type Context = Context<Self>;
9+
}
10+
11+
// ---- Messages ----
12+
13+
pub struct MsgIncrement;
14+
15+
impl Message for MsgIncrement {
16+
type Result = u32;
17+
}
18+
19+
// ---- Handlers ----
20+
21+
impl Handler<MsgIncrement> for CountActor {
22+
type Result = u32;
23+
24+
fn handle(&mut self, _: MsgIncrement, _: &mut Context<Self>) -> Self::Result {
25+
self.0 += 1;
26+
self.0
27+
}
28+
}
+42-39
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,66 @@
1+
use actix::prelude::*;
12
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;
106

117
use shared;
128

13-
type State = Arc<Mutex<StateData>>;
9+
mod count_actor;
10+
use count_actor::{CountActor, MsgIncrement};
1411

15-
#[derive(Default)]
16-
struct StateData {
17-
message_ordinal_number: u32,
18-
}
12+
// ---- Apis ("/api/*") ----
1913

20-
#[post("/api/send-message")]
14+
#[post("send-message")]
2115
fn send_message(
2216
state: web::Data<State>,
2317
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+
})
3029
}
3130

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)))
3637
}
3738

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>,
4641
}
4742

4843
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();
5047

5148
HttpServer::new(move || {
5249
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+
)
5758
.service(Files::new("/public", "./client/public"))
5859
.service(Files::new("/pkg", "./client/pkg"))
59-
.service(index)
60+
.default_service(web::get().to(|| NamedFile::open("./client/index.html")))
6061
})
6162
.bind("127.0.0.1:8000")?
62-
.run()
63+
.run()?;
64+
65+
system.run()
6366
}

0 commit comments

Comments
 (0)