From b080cd46699d8ee63b4a07a6d7f353f3a51dbf0b Mon Sep 17 00:00:00 2001
From: duyanhehe <duyanhex@gmail.com>
Date: Wed, 5 Mar 2025 09:05:02 +0700
Subject: [PATCH] fix config

---
 .env.example            |  1 +
 app/backend/database.py |  2 +-
 app/backend/main.py     |  2 +-
 app/core/config.py      | 26 +++++++++++++-------------
 4 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/.env.example b/.env.example
index 6a3cb37..56e7c06 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 979b340..d394b3c 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 b7c46af..d03cae7 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 0afa9ae..d61a265 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()
-- 
GitLab