Skip to main content

aonyx_api/
server.rs

1//! Bind + serve the API router.
2
3use aonyx_core::{AonyxError, Result};
4
5use crate::build_router;
6use crate::state::ApiState;
7
8/// Bind `addr` (e.g. `127.0.0.1:8788`) and serve the full API until the
9/// process is stopped. The binary wires this from `aonyx serve api`.
10pub async fn serve(state: ApiState, addr: &str) -> Result<()> {
11    let app = build_router(state);
12    let listener = tokio::net::TcpListener::bind(addr)
13        .await
14        .map_err(|e| AonyxError::Adapter(format!("bind {addr}: {e}")))?;
15    axum::serve(listener, app)
16        .await
17        .map_err(|e| AonyxError::Adapter(format!("serve: {e}")))?;
18    Ok(())
19}