Skip to content

Commit

Permalink
Adjusts and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano Santos committed Feb 12, 2023
1 parent 31fea3e commit 6f3bbdc
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 23 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spawn-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
7 changes: 3 additions & 4 deletions spawn-examples/src/joe.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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())
Expand Down
7 changes: 5 additions & 2 deletions spawn-examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions spawn-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
authors = ["Adriano Santos <solid.sistemas@gmail.com>", "Weslei Pereira"]
description = "Spawn Rust SDK"
documentation = "https://github.com/eigr-labs/spawn-rust-sdk"
edition = "2021"
homepage = "https://eigr.io/"
keywords = [
"actors",
Expand All @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions spawn-rs/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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;
}
10 changes: 10 additions & 0 deletions spawn-rs/src/handler/callback.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<Box<dyn Actor>>>, _req: HttpRequest) -> impl Responder {
"Hello World from v1 API!"
}
}
1 change: 1 addition & 0 deletions spawn-rs/src/handler/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod callback;
2 changes: 2 additions & 0 deletions spawn-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extern crate actix_web;
extern crate prost_types;

mod eigr {
Expand All @@ -11,5 +12,6 @@ mod eigr {
pub mod action;
pub mod actor;
pub mod context;
pub mod handler;
pub mod spawn;
pub mod value;
61 changes: 50 additions & 11 deletions spawn-rs/src/spawn.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,79 @@
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<Box<dyn Actor>>,
actors: Vec<Box<dyn Actor>>,
server_port: u16,
}

impl Default for Spawn {
fn default() -> Spawn {
Spawn {
actor: Vec::new(),
actors: Vec::new(),
server_port: 8091,
system: String::from(""),
}
}
}

impl Spawn {
pub fn new() -> Self {
Default::default()
pub fn add_actor(&mut self, actor: Box<dyn Actor>) -> &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<Box<dyn Actor>> {
&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 {
self.server_port = server_port;
self
}

pub fn add_actor(&mut self, actor: Box<dyn Actor>) -> &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);
}
}

0 comments on commit 6f3bbdc

Please sign in to comment.