use ajj::{
pubsub::{
ipc::{to_name, ListenerOptions},
Connect,
},
HandlerCtx, Router,
};
use tempfile::NamedTempFile;
#[tokio::main]
async fn main() -> eyre::Result<()> {
let router = make_router();
let tempfile = NamedTempFile::new()?;
let name = to_name(tempfile.path().as_os_str()).expect("invalid name");
println!("Serving IPC on socket: {:?}", tempfile.path());
println!("use Ctrl-C to stop");
let guard = ListenerOptions::new().name(name).serve(router).await?;
tokio::signal::ctrl_c().await?;
drop(guard);
Ok(())
}
fn make_router() -> Router<()> {
Router::<()>::new()
.route("helloWorld", || async {
tracing::info!("serving hello world");
Ok::<_, ()>("Hello, world!")
})
.route("addNumbers", |(a, b): (u32, u32)| async move {
tracing::info!("serving addNumbers");
Ok::<_, ()>(a + b)
})
.route("notify", |ctx: HandlerCtx| async move {
if !ctx.notifications_enabled() {
return Err("notifications are disabled");
}
let req_id = 15u8;
ctx.spawn_with_ctx(|ctx| async move {
let result = 100_000_000;
let _ = ctx
.notify(&serde_json::json!({
"req_id": req_id,
"result": result,
}))
.await;
});
Ok(req_id)
})
}