diff --git a/.env.example b/.env.example index 6a3cb37fdf2fe2ed60e51e29867137becc234a9a..56e7c069252d39c8ba1059a0de2ae412634f5599 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,6 @@ # Copy this file to .env +APP_NAME = Shopping App # Change to your own database DATABASE_URL = mysql+pymysql://your_user:your_password@localhost/shopping # This template use mysql # change your_user to your mysql username diff --git a/app/backend/database.py b/app/backend/database.py index 979b3409ba75b6680f1a553170208e32989bdd0a..d394b3c74e3b02c20656f0708626169bb9e33ec8 100644 --- a/app/backend/database.py +++ b/app/backend/database.py @@ -1,7 +1,7 @@ from sqlmodel import SQLModel, Session, create_engine from app.core.config import settings -engine = create_engine(settings.DATABASE_URL, echo=settings.DEBUG) +engine = create_engine(settings.database_url, echo=settings.debug) def init_db(): diff --git a/app/backend/main.py b/app/backend/main.py index b7c46af1b5ca277f6dd236b0462e9ca5b0ddb26a..d03cae7ea6b458687e22d798b4f8d3da73827ec3 100644 --- a/app/backend/main.py +++ b/app/backend/main.py @@ -8,7 +8,7 @@ from backend.routes import auth from backend.database import init_db from core.config import settings -app = FastAPI(title="Shopping App", version="1.0.0", debug=settings.DEBUG) +app = FastAPI(title="Shopping App", version="1.0.0", debug=settings.debug) # initialize database init_db() diff --git a/app/core/config.py b/app/core/config.py index 0afa9ae553d0cd06caf73d77eea79e7d8538bf95..d61a265ca889abfe20de8b39a82cfc5595178f10 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -1,19 +1,19 @@ -import os -from dotenv import load_dotenv +from pathlib import Path +from pydantic_settings import BaseSettings, SettingsConfigDict -# Load environment variables from .env (located outside /app/) -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -ENV_PATH = os.path.join(BASE_DIR, ".env") -load_dotenv(ENV_PATH) +class Settings(BaseSettings): + app_name: str = "Shopping App" + database_url: str # Ensure lowercase to match the .env key + secret_key: str + debug: bool = True # Lowercase to match Python conventions -class Settings: - DATABASE_URL: str = os.getenv("DATABASE_URL") - SECRET_KEY: str = os.getenv("SECRET_KEY") - DEBUG: bool = os.getenv("DEBUG", "False").lower() in ["true", "1"] - - if DATABASE_URL is None: - raise ValueError("DATABASE_URL is not set or empty") + model_config = SettingsConfigDict( + env_file=str(Path(__file__).resolve().parent.parent / ".env"), + env_file_encoding="utf-8", + case_sensitive=False, # Allows case-insensitive environment variable keys + ) +# Create a singleton instance to use in the app settings = Settings()