Skip to content
Snippets Groups Projects
Commit b080cd46 authored by duyanhehe's avatar duyanhehe
Browse files

fix config

parent b99e0da6
No related branches found
No related tags found
No related merge requests found
# 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
......
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():
......
......@@ -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()
......
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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment