Files
client/crates/forage-server/src/serve_http.rs
2026-03-15 22:38:42 +01:00

38 lines
938 B
Rust

use std::net::SocketAddr;
use anyhow::Context;
use notmad::{Component, ComponentInfo, MadError};
use tokio_util::sync::CancellationToken;
use crate::state::AppState;
pub struct ServeHttp {
pub addr: SocketAddr,
pub state: AppState,
}
impl Component for ServeHttp {
fn info(&self) -> ComponentInfo {
"forage/http".into()
}
async fn run(&self, cancellation_token: CancellationToken) -> Result<(), MadError> {
let app = crate::build_router(self.state.clone());
let listener = tokio::net::TcpListener::bind(self.addr)
.await
.context("failed to listen on port")?;
tracing::info!("listening on {}", self.addr);
axum::serve(listener, app)
.with_graceful_shutdown(async move {
cancellation_token.cancelled().await;
})
.await
.context("failed to run axum server")?;
Ok(())
}
}