feat: expand workspace to include libertas_infra and libertas_worker
feat(libertas_api): add dependency on libertas_infra and async-nats refactor(libertas_api): consolidate config loading and add broker_url refactor(libertas_api): integrate NATS client into app state and services feat(libertas_core): introduce config module for database and server settings fix(libertas_core): enhance error handling with detailed messages feat(libertas_infra): create infrastructure layer with database repositories feat(libertas_infra): implement Postgres repositories for media and albums feat(libertas_worker): add worker service to process media jobs via NATS
This commit is contained in:
75
libertas_infra/src/factory.rs
Normal file
75
libertas_infra/src/factory.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use libertas_core::{
|
||||
config::{DatabaseConfig, DatabaseType},
|
||||
error::{CoreError, CoreResult},
|
||||
repositories::UserRepository,
|
||||
};
|
||||
use sqlx::{Pool, Postgres, Sqlite};
|
||||
|
||||
use crate::repositories::user_repository::{PostgresUserRepository, SqliteUserRepository};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum DatabasePool {
|
||||
Postgres(Pool<Postgres>),
|
||||
Sqlite(Pool<Sqlite>),
|
||||
}
|
||||
|
||||
pub async fn build_database_pool(db_config: &DatabaseConfig) -> CoreResult<DatabasePool> {
|
||||
match db_config.db_type {
|
||||
DatabaseType::Postgres => {
|
||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(50)
|
||||
.connect(&db_config.url)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
Ok(DatabasePool::Postgres(pool))
|
||||
}
|
||||
DatabaseType::Sqlite => {
|
||||
let pool = sqlx::sqlite::SqlitePoolOptions::new()
|
||||
.max_connections(2)
|
||||
.connect(&db_config.url)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
Ok(DatabasePool::Sqlite(pool))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn build_user_repository(
|
||||
_db_config: &DatabaseConfig,
|
||||
pool: DatabasePool,
|
||||
) -> CoreResult<Arc<dyn UserRepository>> {
|
||||
match pool {
|
||||
DatabasePool::Postgres(pg_pool) => Ok(Arc::new(PostgresUserRepository::new(pg_pool))),
|
||||
DatabasePool::Sqlite(sqlite_pool) => Ok(Arc::new(SqliteUserRepository::new(sqlite_pool))),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn build_media_repository(
|
||||
_db_config: &DatabaseConfig,
|
||||
pool: DatabasePool,
|
||||
) -> CoreResult<Arc<dyn libertas_core::repositories::MediaRepository>> {
|
||||
match pool {
|
||||
DatabasePool::Postgres(pg_pool) => Ok(Arc::new(
|
||||
crate::repositories::media_repository::PostgresMediaRepository::new(pg_pool),
|
||||
)),
|
||||
DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database(
|
||||
"Sqlite media repository not implemented".to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn build_album_repository(
|
||||
_db_config: &DatabaseConfig,
|
||||
pool: DatabasePool,
|
||||
) -> CoreResult<Arc<dyn libertas_core::repositories::AlbumRepository>> {
|
||||
match pool {
|
||||
DatabasePool::Postgres(pg_pool) => Ok(Arc::new(
|
||||
crate::repositories::album_repository::PostgresAlbumRepository::new(pg_pool),
|
||||
)),
|
||||
DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database(
|
||||
"Sqlite album repository not implemented".to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user