feat: Implement database factory and abstract session store for multi-database support, centralizing service creation in main.rs.

This commit is contained in:
2025-12-25 22:43:05 +01:00
parent 78d9314602
commit b53dbf2ea8
12 changed files with 246 additions and 80 deletions

View File

@@ -0,0 +1,46 @@
use async_trait::async_trait;
use sqlx;
use tower_sessions::{
SessionStore,
session::{Id, Record},
};
use tower_sessions_sqlx_store::{PostgresStore, SqliteStore};
#[derive(Clone, Debug)]
pub enum InfraSessionStore {
Sqlite(SqliteStore),
Postgres(PostgresStore),
}
#[async_trait]
impl SessionStore for InfraSessionStore {
async fn save(&self, session_record: &Record) -> tower_sessions::session_store::Result<()> {
match self {
Self::Sqlite(store) => store.save(session_record).await,
Self::Postgres(store) => store.save(session_record).await,
}
}
async fn load(&self, session_id: &Id) -> tower_sessions::session_store::Result<Option<Record>> {
match self {
Self::Sqlite(store) => store.load(session_id).await,
Self::Postgres(store) => store.load(session_id).await,
}
}
async fn delete(&self, session_id: &Id) -> tower_sessions::session_store::Result<()> {
match self {
Self::Sqlite(store) => store.delete(session_id).await,
Self::Postgres(store) => store.delete(session_id).await,
}
}
}
impl InfraSessionStore {
pub async fn migrate(&self) -> Result<(), sqlx::Error> {
match self {
Self::Sqlite(store) => store.migrate().await,
Self::Postgres(store) => store.migrate().await,
}
}
}