feat: Add initial PostgreSQL migration, refactor database connection to k_core, and simplify session store setup.

This commit is contained in:
2026-01-02 05:47:51 +01:00
parent cb73f146c5
commit c1c42f4fd9
8 changed files with 64 additions and 159 deletions

View File

@@ -1,10 +1,10 @@
use std::net::SocketAddr;
use std::time::Duration as StdDuration;
use template_domain::UserService;
use template_infra::factory::build_user_repository;
use template_infra::{db, session_store};
use k_core::logging;
use template_domain::UserService;
use template_infra::factory::build_session_store;
use template_infra::factory::build_user_repository;
use tokio::net::TcpListener;
use tower_sessions::{Expiry, SessionManagerLayer};
use tracing::info;
@@ -32,43 +32,27 @@ async fn main() -> anyhow::Result<()> {
info!("Starting server on {}:{}", config.host, config.port);
// 3. Connect to database
let db_config = db::DatabaseConfig {
// k-core handles the "Which DB are we using?" logic internally based on feature flags
// and returns the correct Enum variant.
let db_config = k_core::db::DatabaseConfig {
url: config.database_url.clone(),
max_connections: 5,
min_connections: 1,
acquire_timeout: StdDuration::from_secs(30),
};
// We assume generic connection logic in k-core/template-infra
// But here we use k-core via template-infra
#[cfg(feature = "sqlite")]
let pool = k_core::db::connect_sqlite(&db_config.url).await?;
#[cfg(feature = "postgres")]
let pool = k_core::db::connect_postgres(&db_config.url).await?;
#[cfg(feature = "sqlite")]
let db_pool = template_infra::db::DatabasePool::Sqlite(pool.clone());
#[cfg(feature = "postgres")]
let db_pool = template_infra::db::DatabasePool::Postgres(pool.clone());
// Returns k_core::db::DatabasePool
let db_pool = k_core::db::connect(&db_config).await?;
// 4. Run migrations
db::run_migrations(&db_pool).await?;
// 4. Run migrations (using the re-export if you kept it, or direct k_core)
template_infra::db::run_migrations(&db_pool).await?;
// 5. Initialize Services
let user_repo = build_user_repository(&db_pool).await?;
let user_service = UserService::new(user_repo.clone());
// 6. Setup Session Store
#[cfg(feature = "sqlite")]
let session_store = session_store::InfraSessionStore::Sqlite(
tower_sessions_sqlx_store::SqliteStore::new(pool.clone())
);
#[cfg(feature = "postgres")]
let session_store = session_store::InfraSessionStore::Postgres(
tower_sessions_sqlx_store::PostgresStore::new(pool.clone())
);
let session_store = build_session_store(&db_pool).await?;
let session_layer = SessionManagerLayer::new(session_store)
.with_secure(false) // Set to true in production with HTTPS
.with_expiry(Expiry::OnInactivity(time::Duration::hours(1)));
@@ -80,9 +64,7 @@ async fn main() -> anyhow::Result<()> {
let state = AppState::new(user_service, config.clone());
// 9. Build Router
let app = routes::api_v1_router()
.layer(auth_layer)
.with_state(state);
let app = routes::api_v1_router().layer(auth_layer).with_state(state);
// 10. Start Server
let addr: SocketAddr = format!("{}:{}", config.host, config.port).parse()?;