From 5949ffc63b8b06eed43b5bfbc57d4cda73076eba Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Mon, 16 Mar 2026 04:34:08 +0100 Subject: [PATCH] refactor: extract DB init to database.rs --- k-tv-backend/api/src/database.rs | 36 ++++++++++++++++++++++++++++++++ k-tv-backend/api/src/main.rs | 31 ++------------------------- 2 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 k-tv-backend/api/src/database.rs diff --git a/k-tv-backend/api/src/database.rs b/k-tv-backend/api/src/database.rs new file mode 100644 index 0000000..3cc32fb --- /dev/null +++ b/k-tv-backend/api/src/database.rs @@ -0,0 +1,36 @@ +use std::sync::Arc; +use std::time::Duration as StdDuration; + +use crate::config::Config; +use infra::run_migrations; +use k_core::db::DatabasePool; + +pub async fn init_database(config: &Config) -> anyhow::Result> { + tracing::info!("Connecting to database: {}", config.database_url); + + #[cfg(all(feature = "sqlite", not(feature = "postgres")))] + let db_type = k_core::db::DbType::Sqlite; + + #[cfg(all(feature = "postgres", not(feature = "sqlite")))] + let db_type = k_core::db::DbType::Postgres; + + // Both features enabled: fall back to URL inspection at runtime + #[cfg(all(feature = "sqlite", feature = "postgres"))] + let db_type = if config.database_url.starts_with("postgres") { + k_core::db::DbType::Postgres + } else { + k_core::db::DbType::Sqlite + }; + + let db_config = k_core::db::DatabaseConfig { + db_type, + url: config.database_url.clone(), + max_connections: config.db_max_connections, + min_connections: config.db_min_connections, + acquire_timeout: StdDuration::from_secs(30), + }; + + let pool = k_core::db::connect(&db_config).await?; + run_migrations(&pool).await?; + Ok(Arc::new(pool)) +} diff --git a/k-tv-backend/api/src/main.rs b/k-tv-backend/api/src/main.rs index b304a28..e6b4488 100644 --- a/k-tv-backend/api/src/main.rs +++ b/k-tv-backend/api/src/main.rs @@ -4,7 +4,6 @@ use std::net::SocketAddr; use std::sync::Arc; -use std::time::Duration as StdDuration; use axum::Router; use axum::http::{HeaderName, HeaderValue}; @@ -15,11 +14,11 @@ use domain::{ChannelService, IMediaProvider, IProviderRegistry, ProviderCapabili use infra::factory::{build_activity_log_repository, build_channel_repository, build_provider_config_repository, build_schedule_repository, build_user_repository}; #[cfg(feature = "local-files")] use infra::factory::build_transcode_settings_repository; -use infra::run_migrations; use k_core::http::server::{ServerConfig, apply_standard_middleware}; use tokio::net::TcpListener; mod config; +mod database; mod dto; mod error; mod events; @@ -44,33 +43,7 @@ async fn main() -> anyhow::Result<()> { info!("Starting server on {}:{}", config.host, config.port); // Setup database - tracing::info!("Connecting to database: {}", config.database_url); - - #[cfg(all(feature = "sqlite", not(feature = "postgres")))] - let db_type = k_core::db::DbType::Sqlite; - - #[cfg(all(feature = "postgres", not(feature = "sqlite")))] - let db_type = k_core::db::DbType::Postgres; - - // Both features enabled: fall back to URL inspection at runtime - #[cfg(all(feature = "sqlite", feature = "postgres"))] - let db_type = if config.database_url.starts_with("postgres") { - k_core::db::DbType::Postgres - } else { - k_core::db::DbType::Sqlite - }; - - let db_config = k_core::db::DatabaseConfig { - db_type, - url: config.database_url.clone(), - max_connections: config.db_max_connections, - min_connections: config.db_min_connections, - acquire_timeout: StdDuration::from_secs(30), - }; - - let db_pool = k_core::db::connect(&db_config).await?; - run_migrations(&db_pool).await?; - let db_pool = Arc::new(db_pool); + let db_pool = database::init_database(&config).await?; let user_repo = build_user_repository(&db_pool).await?; let channel_repo = build_channel_repository(&db_pool).await?;