use std::future::Future;
use std::net::SocketAddr;
use lightshuttle_runtime::LifecycleHandle;
use tokio::net::TcpListener;
use crate::routes::router;
use crate::state::ControlState;
pub async fn bind(addr: SocketAddr) -> std::io::Result<TcpListener> {
TcpListener::bind(addr).await
}
pub struct ControlServer<H>
where
H: LifecycleHandle + Clone + Send + Sync + 'static,
{
state: ControlState<H>,
}
impl<H> ControlServer<H>
where
H: LifecycleHandle + Clone + Send + Sync + 'static,
{
#[must_use]
pub fn new(state: ControlState<H>) -> Self {
Self { state }
}
pub fn into_router(self) -> axum::Router {
router(self.state)
}
pub async fn serve<F>(self, listener: TcpListener, shutdown: F) -> std::io::Result<()>
where
F: Future<Output = ()> + Send + 'static,
{
let app = router(self.state);
axum::serve(listener, app)
.with_graceful_shutdown(shutdown)
.await
}
}