Careeroza
Settings & Configuration
pydantic-settings
Read configuration from environment variables (and a .env file) with validation and defaults, using the pydantic-settings package:
# app/core/config.py
from functools import lru_cache
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
app_name: str = "Course API"
database_url: str
jwt_secret: str
debug: bool = False
model_config = SettingsConfigDict(env_file=".env")
@lru_cache
def get_settings() -> Settings:
return Settings() # reads DATABASE_URL, JWT_SECRET, DEBUG from the environmentTip: Inject settings with Depends(get_settings): tests can then override them with app.dependency_overrides.