use ajj::HandlerCtx;
use std::net::SocketAddr;
#[tokio::main]
async fn main() -> eyre::Result<()> {
let router = make_router();
let axum = router.clone().into_axum_with_ws("/", "/ws");
let addr = SocketAddr::from(([127, 0, 0, 1], 0));
let listener = tokio::net::TcpListener::bind(addr).await?;
println!("Listening for POST on {}/", listener.local_addr()?);
println!("Listening for WS on {}/ws", listener.local_addr()?);
println!("use Ctrl-C to stop");
axum::serve(listener, axum).await.map_err(Into::into)
}
fn make_router() -> ajj::Router<()> {
ajj::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)
})
}