From dfeb10c351dbe7497aec6f63c2adb8dbe76aa4f3 Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Fri, 28 Feb 2025 09:21:48 +0100 Subject: [PATCH] Refactor config and database handling. Move configuration logic to a more modular structure and enhance database URL generation using individual components from environment variables. Adjust database initialization to utilize the updated settings for improved maintainability and clarity. --- backend/app/config.py | 29 ---------------------- backend/app/core/config.py | 48 ++++++++++++++++++++++++++++++++++++ backend/app/core/database.py | 6 ++--- 3 files changed, 51 insertions(+), 32 deletions(-) delete mode 100644 backend/app/config.py create mode 100644 backend/app/core/config.py diff --git a/backend/app/config.py b/backend/app/config.py deleted file mode 100644 index 36ceecd..0000000 --- a/backend/app/config.py +++ /dev/null @@ -1,29 +0,0 @@ -from pydantic_settings import BaseSettings -from typing import Optional, List - - -class Settings(BaseSettings): - PROJECT_NAME: Optional[str] = "EventSpace" - VERSION: Optional[str] = "1.0.0" - API_V1_STR: Optional[str] = "/api/v1" - - # Database configuration - DATABASE_URL: Optional[str] = None - - # JWT configuration - SECRET_KEY: Optional[str] = None - ALGORITHM: Optional[str] = "HS256" - ACCESS_TOKEN_EXPIRE_MINUTES: Optional[int] = 30 - - # CORS configuration - BACKEND_CORS_ORIGINS: Optional[List[str]] = ["http://localhost:3000"] # Frontend URL - - # Admin user - FIRST_SUPERUSER_EMAIL: Optional[str] = None - FIRST_SUPERUSER_PASSWORD: Optional[str] = None - - class Config: - env_file = ".env" - - -settings = Settings() \ No newline at end of file diff --git a/backend/app/core/config.py b/backend/app/core/config.py new file mode 100644 index 0000000..27056d6 --- /dev/null +++ b/backend/app/core/config.py @@ -0,0 +1,48 @@ +from pydantic_settings import BaseSettings +from typing import Optional, List + + +class Settings(BaseSettings): + PROJECT_NAME: str = "EventSpace" + VERSION: str = "1.0.0" + API_V1_STR: str = "/api/v1" + + # Database configuration + POSTGRES_USER: str = "postgres" + POSTGRES_PASSWORD: str = "postgres" + POSTGRES_HOST: str = "localhost" + POSTGRES_PORT: str = "5432" + POSTGRES_DB: str = "eventspace" + DATABASE_URL: Optional[str] = None + + @property + def database_url(self) -> str: + """ + Get the SQLAlchemy database URL. + If DATABASE_URL is explicitly set, use that. + Otherwise, construct from components. + """ + if self.DATABASE_URL: + return self.DATABASE_URL + self.DATABASE_URL = f"postgresql://{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}@{self.POSTGRES_HOST}:{self.POSTGRES_PORT}/{self.POSTGRES_DB}" + return self.DATABASE_URL + + # JWT configuration + SECRET_KEY: str = "your_secret_key_here" + ALGORITHM: str = "HS256" + ACCESS_TOKEN_EXPIRE_MINUTES: int = 30 + + # CORS configuration + BACKEND_CORS_ORIGINS: List[str] = ["http://localhost:3000"] + + # Admin user + FIRST_SUPERUSER_EMAIL: Optional[str] = None + FIRST_SUPERUSER_PASSWORD: Optional[str] = None + + class Config: + env_file = ".env" + env_file_encoding = "utf-8" + case_sensitive = True + + +settings = Settings() \ No newline at end of file diff --git a/backend/app/core/database.py b/backend/app/core/database.py index c557614..22d743a 100644 --- a/backend/app/core/database.py +++ b/backend/app/core/database.py @@ -1,11 +1,11 @@ from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker -import os -SQLALCHEMY_DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:postgres@db:5432/eventspace") +from app.core.config import settings -engine = create_engine(SQLALCHEMY_DATABASE_URL) +# Use the database URL from settings +engine = create_engine(settings.database_url) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base()