
Added fallback implementation for environments without pydantic_settings package and added pydantic-settings to requirements.txt to prevent future issues.
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
import os
|
|
from pathlib import Path
|
|
|
|
from pydantic import BaseModel
|
|
|
|
# Try to use pydantic_settings if available, otherwise fallback to a simple BaseModel
|
|
try:
|
|
from pydantic_settings import BaseSettings
|
|
except ImportError:
|
|
# Fallback for environments without pydantic_settings
|
|
# In Pydantic v2, BaseSettings was moved to pydantic_settings package
|
|
class BaseSettings(BaseModel):
|
|
"""
|
|
Simplified BaseSettings implementation for compatibility.
|
|
"""
|
|
class Config:
|
|
env_file = ".env"
|
|
case_sensitive = True
|
|
|
|
def __init__(self, **kwargs):
|
|
# Load environment variables
|
|
env_values = {}
|
|
for field_name, _ in self.__annotations__.items():
|
|
env_val = os.environ.get(field_name)
|
|
if env_val is not None:
|
|
env_values[field_name] = env_val
|
|
|
|
# Override with any provided values
|
|
env_values.update(kwargs)
|
|
super().__init__(**env_values)
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
PROJECT_NAME: str = "Todo API"
|
|
API_V1_STR: str = "/api/v1"
|
|
|
|
# Database settings
|
|
DB_DIR: Path = Path("/app") / "storage" / "db"
|
|
DB_NAME: str = "db.sqlite"
|
|
|
|
class Config:
|
|
env_file = ".env"
|
|
case_sensitive = True
|
|
|
|
|
|
settings = Settings()
|
|
|
|
# Ensure DB directory exists
|
|
settings.DB_DIR.mkdir(parents=True, exist_ok=True) |