diff --git a/Cargo.lock b/Cargo.lock index 81a66d2..a4144ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1032,12 +1032,14 @@ version = "0.1.0" dependencies = [ "prost-types", "spawn-rs", + "tokio", ] [[package]] name = "spawn-rs" version = "0.1.0" dependencies = [ + "actix-server", "actix-web", "prost", "prost-types", @@ -1122,13 +1124,26 @@ dependencies = [ "libc", "memchr", "mio", + "num_cpus", "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2", + "tokio-macros", "windows-sys 0.42.0", ] +[[package]] +name = "tokio-macros" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio-util" version = "0.7.6" diff --git a/spawn-examples/Cargo.toml b/spawn-examples/Cargo.toml index f9f6365..5bae9b8 100644 --- a/spawn-examples/Cargo.toml +++ b/spawn-examples/Cargo.toml @@ -6,3 +6,4 @@ version = "0.1.0" [dependencies] prost-types = "0.11" spawn-rs = {path = "../spawn-rs"} +tokio = {version = "1.25.0", features = ["full"]} diff --git a/spawn-examples/src/joe.rs b/spawn-examples/src/joe.rs index 8960d37..d56598c 100644 --- a/spawn-examples/src/joe.rs +++ b/spawn-examples/src/joe.rs @@ -1,7 +1,7 @@ use prost_types::Any; use spawn_rs::{ - action::{Action, Request}, + action::{Action, Message}, actor::{Actor, ActorSettings, Kind}, context::Context, value::Value, @@ -23,13 +23,12 @@ impl Actor for Joe { } impl Action for Joe { - fn handle(&mut self, req: Request, ctx: &mut Context) -> Value { - match req.action() { + fn handle(&mut self, msg: Message, ctx: &mut Context) -> Value { + match msg.action() { "sum" => Value::new() .state(ctx.state().clone()) .response(Any::default()) .to_owned(), - _ => Value::new() .state(Any::default()) .response(Any::default()) diff --git a/spawn-examples/src/main.rs b/spawn-examples/src/main.rs index d6e1efb..0a5a66f 100644 --- a/spawn-examples/src/main.rs +++ b/spawn-examples/src/main.rs @@ -1,13 +1,16 @@ extern crate prost_types; +extern crate tokio; mod joe; use spawn_rs::spawn::Spawn; -fn main() { +#[tokio::main] +async fn main() { Spawn::new() .system("spawn-system".to_string()) .port(8091) .add_actor(Box::new(joe::Joe {})) - .start(); + .start() + .await; } diff --git a/spawn-rs/Cargo.toml b/spawn-rs/Cargo.toml index 1f83f39..4df838a 100644 --- a/spawn-rs/Cargo.toml +++ b/spawn-rs/Cargo.toml @@ -2,6 +2,7 @@ authors = ["Adriano Santos ", "Weslei Pereira"] description = "Spawn Rust SDK" documentation = "https://github.com/eigr-labs/spawn-rust-sdk" +edition = "2021" homepage = "https://eigr.io/" keywords = [ "actors", @@ -13,6 +14,7 @@ name = "spawn-rs" version = "0.1.0" [dependencies] +actix-server = "2.2.0" actix-web = "4" prost = "0.11" prost-types = "0.11" diff --git a/spawn-rs/src/action.rs b/spawn-rs/src/action.rs index 78c8aa6..11901fb 100644 --- a/spawn-rs/src/action.rs +++ b/spawn-rs/src/action.rs @@ -3,21 +3,21 @@ use prost_types::Any; use crate::{actor::Actor, context::Context, value::Value}; #[derive(Debug, Clone)] -pub struct Request { +pub struct Message { action: String, body: Any, } -impl Default for Request { - fn default() -> Request { - Request { +impl Default for Message { + fn default() -> Message { + Message { action: String::from(""), body: Any::default(), } } } -impl Request { +impl Message { pub fn new() -> Self { Default::default() } @@ -37,5 +37,5 @@ where Self: Actor, { /// This method is called for every message received by this actor. - fn handle(&mut self, req: Request, ctx: &mut Context) -> Value; + fn handle(&mut self, req: Message, ctx: &mut Context) -> Value; } diff --git a/spawn-rs/src/handler/callback.rs b/spawn-rs/src/handler/callback.rs new file mode 100644 index 0000000..0de2c9d --- /dev/null +++ b/spawn-rs/src/handler/callback.rs @@ -0,0 +1,10 @@ +pub mod v1 { + use crate::actor::Actor; + + use actix_web::{post, web::Data, HttpRequest, Responder}; + + #[post("/actions")] + pub async fn handle(_data: Data>>, _req: HttpRequest) -> impl Responder { + "Hello World from v1 API!" + } +} diff --git a/spawn-rs/src/handler/mod.rs b/spawn-rs/src/handler/mod.rs new file mode 100644 index 0000000..b5da7c5 --- /dev/null +++ b/spawn-rs/src/handler/mod.rs @@ -0,0 +1 @@ +pub mod callback; diff --git a/spawn-rs/src/lib.rs b/spawn-rs/src/lib.rs index 1bf2115..efa5653 100644 --- a/spawn-rs/src/lib.rs +++ b/spawn-rs/src/lib.rs @@ -1,3 +1,4 @@ +extern crate actix_web; extern crate prost_types; mod eigr { @@ -11,5 +12,6 @@ mod eigr { pub mod action; pub mod actor; pub mod context; +pub mod handler; pub mod spawn; pub mod value; diff --git a/spawn-rs/src/spawn.rs b/spawn-rs/src/spawn.rs index de05a46..5dee2c6 100644 --- a/spawn-rs/src/spawn.rs +++ b/spawn-rs/src/spawn.rs @@ -1,15 +1,24 @@ -use actor::Actor; +use crate::actor::Actor; +use crate::handler::callback; + +use std::io::Result; + +use actix_web::{ + middleware, + web::{self}, + App, HttpServer, +}; pub struct Spawn { system: String, - actor: Vec>, + actors: Vec>, server_port: u16, } impl Default for Spawn { fn default() -> Spawn { Spawn { - actor: Vec::new(), + actors: Vec::new(), server_port: 8091, system: String::from(""), } @@ -17,13 +26,25 @@ impl Default for Spawn { } impl Spawn { - pub fn new() -> Self { - Default::default() + pub fn add_actor(&mut self, actor: Box) -> &mut Spawn { + self.actors.push(actor); + self } - pub fn system(&mut self, system_name: String) -> &mut Spawn { - self.system = system_name; - self + pub fn get_actors(&mut self) -> &mut Vec> { + &mut self.actors + } + + pub fn get_port(&mut self) -> u16 { + self.server_port + } + + pub fn get_system(&mut self) -> &mut String { + &mut self.system + } + + pub fn new() -> Self { + Default::default() } pub fn port(&mut self, server_port: u16) -> &mut Spawn { @@ -31,10 +52,28 @@ impl Spawn { self } - pub fn add_actor(&mut self, actor: Box) -> &mut Spawn { - self.actor.push(actor); + pub fn system(&mut self, system_name: String) -> &mut Spawn { + self.system = system_name; self } - pub fn start(&mut self) {} + pub async fn start(&mut self) -> Result<()> { + let server = HttpServer::new(move || { + App::new() + //.app_data(self.get_actors()) + .wrap(middleware::Logger::default()) + .configure(Self::config) + }) + .bind(("127.0.0.1", self.get_port()))? + .run(); + + //future::join(server, another_func()).await; + server.await + } + + fn config(conf: &mut web::ServiceConfig) { + let scope = web::scope("/api/v1/actors").service(callback::v1::handle); + + conf.service(scope); + } }