template
This commit is contained in:
83
backend/config.py
Normal file
83
backend/config.py
Normal file
@@ -0,0 +1,83 @@
|
||||
"""
|
||||
=============================================================================
|
||||
CONFIGURATION MODULE
|
||||
=============================================================================
|
||||
|
||||
This module handles all configuration using Pydantic Settings.
|
||||
|
||||
Benefits of using Pydantic Settings:
|
||||
- Type validation for environment variables
|
||||
- Auto-loading from .env files
|
||||
- IDE autocomplete and type hints
|
||||
- Clear documentation of all required config
|
||||
|
||||
Usage:
|
||||
from config import settings
|
||||
print(settings.OPENAI_API_KEY)
|
||||
"""
|
||||
|
||||
from pydantic_settings import BaseSettings
|
||||
from functools import lru_cache
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""
|
||||
Application settings loaded from environment variables.
|
||||
|
||||
All settings can be overridden via:
|
||||
1. Environment variables
|
||||
2. .env file in the backend directory
|
||||
"""
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# Server Configuration
|
||||
# -------------------------------------------------------------------------
|
||||
PORT: int = 8000
|
||||
ENVIRONMENT: str = "development" # "development", "staging", "production"
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# Supabase Configuration
|
||||
# -------------------------------------------------------------------------
|
||||
SUPABASE_URL: str = ""
|
||||
SUPABASE_SERVICE_KEY: str = "" # Service key for admin access
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# AI Configuration
|
||||
# -------------------------------------------------------------------------
|
||||
OPENAI_API_KEY: str = ""
|
||||
ANTHROPIC_API_KEY: str = ""
|
||||
AI_PROVIDER: str = "openai" # "openai" or "anthropic"
|
||||
AI_MODEL: str = "gpt-3.5-turbo"
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# CORS Configuration
|
||||
# -------------------------------------------------------------------------
|
||||
CORS_ORIGINS: str = "http://localhost:3000" # Comma-separated list
|
||||
|
||||
@property
|
||||
def cors_origins_list(self) -> list[str]:
|
||||
"""Parse CORS_ORIGINS string into a list."""
|
||||
return [origin.strip() for origin in self.CORS_ORIGINS.split(",")]
|
||||
|
||||
class Config:
|
||||
# Load from .env file
|
||||
env_file = ".env"
|
||||
env_file_encoding = "utf-8"
|
||||
# Make field names case-insensitive
|
||||
case_sensitive = False
|
||||
|
||||
|
||||
# Use lru_cache to create a singleton settings instance
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
"""
|
||||
Get cached settings instance.
|
||||
|
||||
Using lru_cache ensures we only read the .env file once,
|
||||
improving performance.
|
||||
"""
|
||||
return Settings()
|
||||
|
||||
|
||||
# Export settings instance for easy import
|
||||
settings = get_settings()
|
||||
Reference in New Issue
Block a user