1
0
distributed-systems-flyio/src/main.rs

45 lines
1.1 KiB
Rust
Raw Normal View History

2024-01-01 20:13:11 +00:00
#![allow(special_module_name)]
2024-01-02 22:01:13 +00:00
use std::{
io::{stdout, Write},
rc::Rc,
sync::{Arc, Mutex},
};
2024-01-01 20:13:11 +00:00
2024-01-02 22:01:13 +00:00
use distributed_systems_flyio::{
types::{EchoMessage, Message, MessageBody},
Malestorm,
};
2024-01-01 20:13:11 +00:00
2024-01-01 19:32:01 +00:00
fn main() {
2024-01-02 22:01:13 +00:00
let mut program = Malestorm::default();
let stdout = Arc::new(stdout());
let handlers = &mut program.handlers;
handlers.insert(
"echo",
Box::new(move |mut msg: Message| {
let message_body: EchoMessage = serde_json::from_value(msg.body.message_body)
.expect("error in parsing message body");
msg.body.message_type = "echo_ok".into();
msg.body.message_body = serde_json::to_value(message_body).unwrap();
match serde_json::to_vec(&msg).map(|buf| {
let mut writer = stdout.lock();
writer.write_all(&buf).expect("error in writing content");
writer.flush()
}) {
Ok(_) => (),
Err(e) => {
eprintln!("error in writing response: {}", e);
}
};
}),
);
program.run();
2024-01-01 19:32:01 +00:00
}