mod routes; use std::sync::Arc; use axum::Router; use tower_http::cors::CorsLayer; use domain::{ConfigRepository, EventPublisher}; pub struct AppState { pub config: Arc, pub events: Arc, } impl Clone for AppState { fn clone(&self) -> Self { Self { config: self.config.clone(), events: self.events.clone(), } } } pub fn router(state: AppState) -> Router where C: ConfigRepository + Send + Sync + 'static, C::Error: std::fmt::Debug + Send, E: EventPublisher + Send + Sync + 'static, E::Error: std::fmt::Debug + Send, { Router::new() .nest("/api", routes::api_routes()) .layer(CorsLayer::permissive()) .with_state(state) } pub async fn serve(addr: &str, state: AppState) -> Result<(), std::io::Error> where C: ConfigRepository + Send + Sync + 'static, C::Error: std::fmt::Debug + Send, E: EventPublisher + Send + Sync + 'static, E::Error: std::fmt::Debug + Send, { let app = router(state); let listener = tokio::net::TcpListener::bind(addr).await?; axum::serve(listener, app).await }