fixing bugs
This commit is contained in:
@@ -4,11 +4,16 @@ use sqlx::PgPool;
|
||||
|
||||
use crate::domain::services::auth_service::AuthService;
|
||||
use crate::application::services::auth_application_service::AuthApplicationService;
|
||||
use crate::application::services::folder_service::FolderService;
|
||||
use crate::infrastructure::repositories::{UserPgRepository, SessionPgRepository};
|
||||
use crate::common::config::AppConfig;
|
||||
use crate::common::di::AuthServices;
|
||||
|
||||
pub async fn create_auth_services(config: &AppConfig, pool: Arc<PgPool>) -> Result<AuthServices> {
|
||||
pub async fn create_auth_services(
|
||||
config: &AppConfig,
|
||||
pool: Arc<PgPool>,
|
||||
folder_service: Option<Arc<FolderService>>
|
||||
) -> Result<AuthServices> {
|
||||
// Crear servicio de dominio de autenticación
|
||||
let auth_service = Arc::new(AuthService::new(
|
||||
config.auth.jwt_secret.clone(),
|
||||
@@ -21,11 +26,19 @@ pub async fn create_auth_services(config: &AppConfig, pool: Arc<PgPool>) -> Resu
|
||||
let session_repository = Arc::new(SessionPgRepository::new(pool.clone()));
|
||||
|
||||
// Crear servicio de aplicación de autenticación
|
||||
let auth_application_service = Arc::new(AuthApplicationService::new(
|
||||
let mut auth_app_service = AuthApplicationService::new(
|
||||
user_repository,
|
||||
session_repository,
|
||||
auth_service.clone(),
|
||||
));
|
||||
);
|
||||
|
||||
// Configurar servicio de carpetas si está disponible
|
||||
if let Some(folder_svc) = folder_service {
|
||||
auth_app_service = auth_app_service.with_folder_service(folder_svc);
|
||||
}
|
||||
|
||||
// Empaquetar servicio en Arc
|
||||
let auth_application_service = Arc::new(auth_app_service);
|
||||
|
||||
Ok(AuthServices {
|
||||
auth_service,
|
||||
|
||||
@@ -209,7 +209,8 @@ pub struct DatabaseConfig {
|
||||
impl Default for DatabaseConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
connection_string: "postgres://postgres:postgres@localhost/oxicloud".to_string(),
|
||||
// Updated connection string with default credentials that PostgreSQL often uses
|
||||
connection_string: "postgres://postgres:postgres@localhost:5432/postgres".to_string(),
|
||||
max_connections: 20,
|
||||
min_connections: 5,
|
||||
connect_timeout_secs: 10,
|
||||
@@ -252,7 +253,7 @@ pub struct FeaturesConfig {
|
||||
impl Default for FeaturesConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enable_auth: false,
|
||||
enable_auth: true, // Enable authentication by default
|
||||
enable_user_storage_quotas: false,
|
||||
enable_file_sharing: false,
|
||||
}
|
||||
|
||||
+81
-14
@@ -4,21 +4,88 @@ use std::time::Duration;
|
||||
use crate::common::config::AppConfig;
|
||||
|
||||
pub async fn create_database_pool(config: &AppConfig) -> Result<PgPool> {
|
||||
tracing::info!("Inicializando conexión a PostgreSQL...");
|
||||
tracing::info!("Inicializando conexión a PostgreSQL con URL: {}",
|
||||
config.database.connection_string.replace("postgres://", "postgres://[user]:[pass]@"));
|
||||
|
||||
// Crear el pool de conexiones con las opciones de configuración
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(config.database.max_connections)
|
||||
.min_connections(config.database.min_connections)
|
||||
.acquire_timeout(Duration::from_secs(config.database.connect_timeout_secs))
|
||||
.idle_timeout(Duration::from_secs(config.database.idle_timeout_secs))
|
||||
.max_lifetime(Duration::from_secs(config.database.max_lifetime_secs))
|
||||
.connect(&config.database.connection_string)
|
||||
.await?;
|
||||
// Add a more robust connection attempt with retries
|
||||
let mut attempt = 0;
|
||||
const MAX_ATTEMPTS: usize = 3;
|
||||
|
||||
// Verificar la conexión
|
||||
sqlx::query("SELECT 1").execute(&pool).await?;
|
||||
while attempt < MAX_ATTEMPTS {
|
||||
attempt += 1;
|
||||
tracing::info!("Intento de conexión a PostgreSQL #{}", attempt);
|
||||
|
||||
// Crear el pool de conexiones con las opciones de configuración
|
||||
match PgPoolOptions::new()
|
||||
.max_connections(config.database.max_connections)
|
||||
.min_connections(config.database.min_connections)
|
||||
.acquire_timeout(Duration::from_secs(config.database.connect_timeout_secs))
|
||||
.idle_timeout(Duration::from_secs(config.database.idle_timeout_secs))
|
||||
.max_lifetime(Duration::from_secs(config.database.max_lifetime_secs))
|
||||
.connect(&config.database.connection_string)
|
||||
.await {
|
||||
Ok(pool) => {
|
||||
// Verificar la conexión
|
||||
match sqlx::query("SELECT 1").execute(&pool).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Conexión a PostgreSQL establecida correctamente");
|
||||
return Ok(pool);
|
||||
},
|
||||
Err(e) => {
|
||||
tracing::error!("Error al verificar conexión: {}", e);
|
||||
// Try creating the tables in this case - might be missing schema
|
||||
tracing::info!("Intentando crear las tablas necesarias...");
|
||||
|
||||
// Simple schema creation - this handles fresh installations
|
||||
let create_tables_result = sqlx::query(r#"
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id TEXT PRIMARY KEY,
|
||||
username TEXT UNIQUE NOT NULL,
|
||||
email TEXT UNIQUE NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
role TEXT NOT NULL,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
quota_bytes BIGINT NOT NULL DEFAULT 1073741824,
|
||||
last_login TIMESTAMP,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id TEXT NOT NULL REFERENCES users(id),
|
||||
refresh_token TEXT UNIQUE NOT NULL,
|
||||
ip_address TEXT,
|
||||
user_agent TEXT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMP NOT NULL,
|
||||
is_revoked BOOLEAN NOT NULL DEFAULT FALSE
|
||||
);
|
||||
"#).execute(&pool).await;
|
||||
|
||||
match create_tables_result {
|
||||
Ok(_) => {
|
||||
tracing::info!("Tablas creadas correctamente");
|
||||
return Ok(pool);
|
||||
},
|
||||
Err(table_err) => {
|
||||
tracing::error!("Error al crear tablas: {}", table_err);
|
||||
if attempt >= MAX_ATTEMPTS {
|
||||
return Err(anyhow::anyhow!("Error en la conexión a PostgreSQL: {}", table_err));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
tracing::error!("Error al conectar a PostgreSQL: {}", e);
|
||||
if attempt >= MAX_ATTEMPTS {
|
||||
return Err(anyhow::anyhow!("Error en la conexión a PostgreSQL: {}", e));
|
||||
}
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!("Conexión a PostgreSQL establecida correctamente");
|
||||
Ok(pool)
|
||||
Err(anyhow::anyhow!("No se pudo establecer la conexión a PostgreSQL después de {} intentos", MAX_ATTEMPTS))
|
||||
}
|
||||
Reference in New Issue
Block a user