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:
@@ -1,25 +1,7 @@
|
||||
use libertas_core::error::CoreResult;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Clone)]
|
||||
pub enum DatabaseType {
|
||||
Postgres,
|
||||
Sqlite,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone)]
|
||||
pub struct DatabaseConfig {
|
||||
pub db_type: DatabaseType,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone)]
|
||||
pub struct Config {
|
||||
pub database: DatabaseConfig,
|
||||
pub server_address: String,
|
||||
pub jwt_secret: String,
|
||||
pub media_library_path: String,
|
||||
}
|
||||
use libertas_core::{
|
||||
config::{Config, DatabaseConfig, DatabaseType},
|
||||
error::CoreResult,
|
||||
};
|
||||
|
||||
pub fn load_config() -> CoreResult<Config> {
|
||||
Ok(Config {
|
||||
@@ -30,5 +12,6 @@ pub fn load_config() -> CoreResult<Config> {
|
||||
server_address: "127.0.0.1:8080".to_string(),
|
||||
jwt_secret: "super_secret_jwt_key".to_string(),
|
||||
media_library_path: "media_library".to_string(),
|
||||
broker_url: "nats://localhost:4222".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use libertas_core::{
|
||||
config::Config,
|
||||
error::{CoreError, CoreResult},
|
||||
repositories::UserRepository,
|
||||
};
|
||||
use sqlx::{Pool, Postgres, Sqlite};
|
||||
use libertas_infra::factory::{
|
||||
build_album_repository, build_database_pool, build_media_repository, build_user_repository,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
config::{Config, DatabaseConfig, DatabaseType},
|
||||
repositories::user_repository::{PostgresUserRepository, SqliteUserRepository},
|
||||
security::{Argon2Hasher, JwtGenerator},
|
||||
services::{
|
||||
album_service::AlbumServiceImpl, media_service::MediaServiceImpl,
|
||||
@@ -17,13 +17,12 @@ use crate::{
|
||||
state::AppState,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
enum DatabasePool {
|
||||
Postgres(Pool<Postgres>),
|
||||
Sqlite(Pool<Sqlite>),
|
||||
}
|
||||
|
||||
pub async fn build_app_state(config: Config) -> CoreResult<AppState> {
|
||||
let nats_client = async_nats::connect(&config.broker_url)
|
||||
.await
|
||||
.map_err(|e| CoreError::Config(format!("Failed to connect to NATS: {}", e)))?;
|
||||
println!("API connected to NATS at {}", config.broker_url);
|
||||
|
||||
let db_pool = build_database_pool(&config.database).await?;
|
||||
|
||||
let user_repo = build_user_repository(&config.database, db_pool.clone()).await?;
|
||||
@@ -34,7 +33,11 @@ pub async fn build_app_state(config: Config) -> CoreResult<AppState> {
|
||||
let tokenizer = Arc::new(JwtGenerator::new(config.jwt_secret.clone()));
|
||||
|
||||
let user_service = Arc::new(UserServiceImpl::new(user_repo, hasher, tokenizer.clone()));
|
||||
let media_service = Arc::new(MediaServiceImpl::new(media_repo.clone(), config.clone()));
|
||||
let media_service = Arc::new(MediaServiceImpl::new(
|
||||
media_repo.clone(),
|
||||
config.clone(),
|
||||
nats_client.clone(),
|
||||
));
|
||||
let album_service = Arc::new(AlbumServiceImpl::new(album_repo, media_repo));
|
||||
|
||||
Ok(AppState {
|
||||
@@ -42,64 +45,6 @@ pub async fn build_app_state(config: Config) -> CoreResult<AppState> {
|
||||
media_service,
|
||||
album_service,
|
||||
token_generator: tokenizer,
|
||||
nats_client,
|
||||
})
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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))),
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,26 +4,35 @@ use async_trait::async_trait;
|
||||
use chrono::Datelike;
|
||||
use futures::stream::StreamExt;
|
||||
use libertas_core::{
|
||||
config::Config,
|
||||
error::{CoreError, CoreResult},
|
||||
models::Media,
|
||||
repositories::MediaRepository,
|
||||
schema::UploadMediaData,
|
||||
services::MediaService,
|
||||
};
|
||||
use serde_json::json;
|
||||
use sha2::{Digest, Sha256};
|
||||
use tokio::{fs, io::AsyncWriteExt};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
pub struct MediaServiceImpl {
|
||||
repo: Arc<dyn MediaRepository>,
|
||||
config: Config,
|
||||
nats_client: async_nats::Client,
|
||||
}
|
||||
|
||||
impl MediaServiceImpl {
|
||||
pub fn new(repo: Arc<dyn MediaRepository>, config: Config) -> Self {
|
||||
Self { repo, config }
|
||||
pub fn new(
|
||||
repo: Arc<dyn MediaRepository>,
|
||||
config: Config,
|
||||
nats_client: async_nats::Client,
|
||||
) -> Self {
|
||||
Self {
|
||||
repo,
|
||||
config,
|
||||
nats_client,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,6 +98,12 @@ impl MediaService for MediaServiceImpl {
|
||||
|
||||
self.repo.create(&media_model).await?;
|
||||
|
||||
let job_payload = json!({ "media_id": media_model.id });
|
||||
self.nats_client
|
||||
.publish("media.new".to_string(), job_payload.to_string().into())
|
||||
.await
|
||||
.map_err(|e| CoreError::Unknown(format!("Failed to publish NATS job: {}", e)))?;
|
||||
|
||||
Ok(media_model)
|
||||
}
|
||||
|
||||
|
||||
@@ -10,4 +10,5 @@ pub struct AppState {
|
||||
pub media_service: Arc<dyn MediaService>,
|
||||
pub album_service: Arc<dyn AlbumService>,
|
||||
pub token_generator: Arc<dyn TokenGenerator>,
|
||||
pub nats_client: async_nats::Client,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user