Files
hack-nation/backend/config.py

84 lines
2.7 KiB
Python
Raw Normal View History

2026-02-03 21:58:25 +01:00
"""
=============================================================================
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()