use sqlx::PgPool; use tokio::net::TcpListener; use tower_http::cors::{AllowOrigin, CorsLayer}; mod config; mod application; mod logging; mod domain; mod http; mod infrastructure; mod state; #[cfg(test)] mod test_support; use crate::{ config::Config, logging::Logger, infrastructure::{ oauth::build_oauth_dependencies, whisper::WhisperClient, }, state::AppState, }; #[tokio::main] async fn main() { let config = Config::from_env().expect("failed to load configuration"); let addr = config.bind_addr.clone(); let logger = Logger::from_config(&config).expect("failed to configure Axiom logger"); let whisper_client = WhisperClient::new(config.clone()); let db_pool = PgPool::connect(&config.database_url) .await .expect("failed to connect to Postgres"); let oauth_dependencies = build_oauth_dependencies(&config, db_pool.clone()); let app_state = AppState { config, whisper_client, logger, db_pool, http_client: oauth_dependencies.http_client, oauth_client_config: oauth_dependencies.oauth_client_config, oauth_request_storage: oauth_dependencies.oauth_request_storage, did_document_storage: oauth_dependencies.did_document_storage, key_resolver: oauth_dependencies.key_resolver, identity_resolver: oauth_dependencies.identity_resolver, oauth_signing_key: oauth_dependencies.oauth_signing_key, }; let app = if cfg!(debug_assertions) { let allowed_origins = [ "http://localhost:4321", "http://localhost:6007", ] .into_iter() .filter_map(|origin| origin.parse().ok()) .collect::>(); let allowed_headers = [ axum::http::header::ACCEPT, axum::http::header::AUTHORIZATION, axum::http::header::CONTENT_TYPE, ]; let allowed_methods = [ axum::http::Method::GET, axum::http::Method::POST, axum::http::Method::OPTIONS, ]; http::routers::router(app_state).layer( CorsLayer::new() .allow_origin(AllowOrigin::list(allowed_origins)) .allow_methods(allowed_methods) .allow_headers(allowed_headers) .allow_credentials(true), ) } else { http::routers::router(app_state) }; let listener = TcpListener::bind(&addr) .await .expect("failed to bind to configured address"); println!("slipnote-backend listening on http://{addr}"); axum::serve(listener, app) .await .expect("axum server failed"); }