-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
what would be an appropriate way to integrate this with a web framework #27
Comments
Hi @evnix this issue was overlooked, my apologies. Riker supports As for your specific case my recommendation is to pass an instance of the Here's an example that uses the Warp HTTP server (which is a really nice functional HTTP server btw): fn main() {
let model: DefaultModel<String> = DefaultModel::new();
let sys = ActorSystem::new(&model).unwrap();
let cfg = AppConfig::from(sys.config());
let actor = sys.actor_of(MyActor::props(), "actor").unwrap();
let h_sys = sys.clone();
let hello = path!("hello" / String)
.map(move|name| {
let res = block_on(ask(&h_sys, &actor, name)).unwrap();
format!("{}", res)
});
warp::serve(hello)
.run(cfg.http_sock);
}
struct MyActor;
impl Actor for MyActor {
type Msg = String;
fn receive(&mut self,
ctx: &Context<Self::Msg>,
msg: Self::Msg,
sender: Option<ActorRef<Self::Msg>>) {
let msg = format!("hello {}", msg);
let _ = sender.try_tell(msg, Some(ctx.myself()));
}
} In this example all requests for Hope that helps! |
Thanks a lot for the in-depth example. |
@leenozara I am trying to follow your example here, and have the following setup. impl Actor for Validator {
type Msg = ValidationMsg;
fn recv(&mut self,
ctx: &Context<Self::Msg>,
msg: Self::Msg,
sender: Sender) {
println!("Received: {:?}", msg);
println!("Sender is: {:?}", sender);
let res = sender.as_ref()
.unwrap()
.try_tell("valid!", Some(ctx.myself().into()));
match res {
Ok(r)=>{
println!("Result: {:?}", r);
},
Err(e)=>{
println!("Error: {:?}", e);
}
}
}
}
#[tokio::main]
async fn main() {
let sys = ActorSystem::new().unwrap();
let validator = sys.sys_actor_of::<Validator>("validator").unwrap();
let cloned_sys = sys.clone();
let verify = warp::post()
.and(warp::path("verify"))
.and(warp::header::header("Authorization"))
.and(warp::body::json())
.map(move |auth_header: String, verify_req: VerifyRequest| {
let msg = ValidationMsg {
token: verify_req.token,
auth_header
};
let res:RemoteHandle<String> = ask(&cloned_sys, &validator, msg);
let answer = block_on(res);
println!("{:?}", answer);
format!("bingo!!")
});
let hello = warp::path!("hello" / String)
.map(|name| format!("Hello, {}!", name));
let routes = hello.or(verify);
warp::serve(routes)
.run(([127, 0, 0, 1], 3030))
.await
} However, the try_tell fails without any message, and the sever never responds. What setup am I missing for the actor to have it successfully reply to the temporary actor? |
|
I have updated my actor and resolved some of the issues, however the impl Actor for Validator {
type Msg = ValidationMsg;
fn recv(&mut self,
ctx: &Context<Self::Msg>,
msg: Self::Msg,
sender: Sender) {
println!("Received: {:?}", msg);
println!("Sender is: {:?}", sender);
match sender {
Some(actor_ref) => {
println!("pulling context");
let myself = Some(ctx.myself().into());
println!("attempting to send");
let res = actor_ref.try_tell("valid!", myself);
match res {
Ok(_x) => {
println!("all good");
},
Err(e) => {
println!("error {:?}", e);
}
}
},
None => {}
}
}
} |
I think it is something to do with my .map(move |auth_header: String, verify_req: VerifyRequest| {
let msg = ValidationMsg {
token: verify_req.token,
auth_header
};
let res:RemoteHandle<String> = ask(&cloned_sys, &validator, msg);
let answer = block_on(res);
println!("{:?}", answer);
format!("bingo!!")
}); Maybe the ask is creating an actor but the msg types dont line up and no answer is delivered? @evnix Did you get this working? I only see your rFrame repo, which doesnt have a dep. on riker. |
I am new to Rust as well as the Actor Pattern on a whole, so I am trying to understand How do i integrate it with an existing web framework.
Should I include the code below within an actor (will it cause any performance issues?)
Should I include the code below within another thread. If so, how do I refer to all the actors.
Basically I am trying to forward all HTTP messages to existing actors that are running.
I did have a look at actix, but it doesn't support named actors(which is big for me) and docs aren't great.
The text was updated successfully, but these errors were encountered: