working version
This commit is contained in:
127
backend/config/settings.py
Normal file → Executable file
127
backend/config/settings.py
Normal file → Executable file
@@ -1,37 +1,118 @@
|
||||
from typing import List
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
class Settings:
|
||||
# Database
|
||||
DATABASE_URL: str = os.getenv("DATABASE_URL", "postgresql://lediscord_user:lediscord_password@postgres:5432/lediscord")
|
||||
"""Configuration principale avec variables d'environnement obligatoires"""
|
||||
|
||||
# JWT
|
||||
JWT_SECRET_KEY: str = "your-super-secret-jwt-key-change-me"
|
||||
JWT_ALGORITHM: str = "HS256"
|
||||
JWT_EXPIRATION_MINUTES: int = 10080 # 7 days
|
||||
# Environnement - OBLIGATOIRE
|
||||
ENVIRONMENT: str = os.getenv("ENVIRONMENT")
|
||||
if not ENVIRONMENT:
|
||||
raise ValueError("ENVIRONMENT variable is required. Use .env.local, .env.development, or .env.production")
|
||||
|
||||
# Debug et reload
|
||||
DEBUG: bool = os.getenv("DEBUG", "false").lower() == "true"
|
||||
RELOAD: bool = os.getenv("RELOAD", "false").lower() == "true"
|
||||
|
||||
# Database - OBLIGATOIRE
|
||||
DATABASE_URL: str = os.getenv("DATABASE_URL")
|
||||
if not DATABASE_URL:
|
||||
raise ValueError("DATABASE_URL variable is required")
|
||||
|
||||
# JWT - OBLIGATOIRE
|
||||
JWT_SECRET_KEY: str = os.getenv("JWT_SECRET_KEY")
|
||||
if not JWT_SECRET_KEY:
|
||||
raise ValueError("JWT_SECRET_KEY variable is required")
|
||||
|
||||
JWT_ALGORITHM: str = os.getenv("JWT_ALGORITHM", "HS256")
|
||||
JWT_EXPIRATION_MINUTES: int = int(os.getenv("JWT_EXPIRATION_MINUTES", "10080"))
|
||||
|
||||
# Upload
|
||||
UPLOAD_PATH: str = "/app/uploads"
|
||||
MAX_UPLOAD_SIZE: int = 100 * 1024 * 1024 # 100MB
|
||||
ALLOWED_IMAGE_TYPES: List[str] = ["image/jpeg", "image/png", "image/gif", "image/webp"]
|
||||
ALLOWED_VIDEO_TYPES: List[str] = ["video/mp4", "video/mpeg", "video/quicktime", "video/webm"]
|
||||
UPLOAD_PATH: str = os.getenv("UPLOAD_PATH", "./uploads")
|
||||
MAX_UPLOAD_SIZE: int = int(os.getenv("MAX_UPLOAD_SIZE", "104857600"))
|
||||
ALLOWED_IMAGE_TYPES: List[str] = os.getenv("ALLOWED_IMAGE_TYPES", "image/jpeg,image/png,image/gif,image/webp").split(",")
|
||||
ALLOWED_VIDEO_TYPES: List[str] = os.getenv("ALLOWED_VIDEO_TYPES", "video/mp4,video/mpeg,video/quicktime,video/webm").split(",")
|
||||
|
||||
# CORS - Fixed list, no environment parsing
|
||||
CORS_ORIGINS: List[str] = ["http://localhost:5173", "http://localhost:3000"]
|
||||
# CORS - OBLIGATOIRE
|
||||
CORS_ORIGINS: str = os.getenv("CORS_ORIGINS")
|
||||
if not CORS_ORIGINS:
|
||||
raise ValueError("CORS_ORIGINS variable is required")
|
||||
CORS_ORIGINS_LIST: List[str] = CORS_ORIGINS.split(",")
|
||||
|
||||
# Email
|
||||
SMTP_HOST: str = "smtp.gmail.com"
|
||||
SMTP_PORT: int = 587
|
||||
SMTP_USER: str = ""
|
||||
SMTP_PASSWORD: str = ""
|
||||
SMTP_FROM: str = "noreply@lediscord.com"
|
||||
SMTP_HOST: str = os.getenv("SMTP_HOST", "smtp.gmail.com")
|
||||
SMTP_PORT: int = int(os.getenv("SMTP_PORT", "587"))
|
||||
SMTP_USER: str = os.getenv("SMTP_USER", "")
|
||||
SMTP_PASSWORD: str = os.getenv("SMTP_PASSWORD", "")
|
||||
SMTP_FROM: str = os.getenv("SMTP_FROM", "noreply@lediscord.com")
|
||||
|
||||
# Admin
|
||||
ADMIN_EMAIL: str = "admin@lediscord.com"
|
||||
ADMIN_PASSWORD: str = "admin123"
|
||||
# Admin - OBLIGATOIRE
|
||||
ADMIN_EMAIL: str = os.getenv("ADMIN_EMAIL")
|
||||
if not ADMIN_EMAIL:
|
||||
raise ValueError("ADMIN_EMAIL variable is required")
|
||||
|
||||
ADMIN_PASSWORD: str = os.getenv("ADMIN_PASSWORD")
|
||||
if not ADMIN_PASSWORD:
|
||||
raise ValueError("ADMIN_PASSWORD variable is required")
|
||||
|
||||
# App
|
||||
APP_NAME: str = "LeDiscord"
|
||||
APP_URL: str = "http://localhost:5173"
|
||||
APP_NAME: str = os.getenv("APP_NAME", "LeDiscord")
|
||||
APP_URL: str = os.getenv("APP_URL", "http://localhost:5173")
|
||||
|
||||
# Logging
|
||||
LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO" if ENVIRONMENT == "production" else "DEBUG")
|
||||
|
||||
# Performance
|
||||
WORKERS: int = int(os.getenv("WORKERS", "1" if ENVIRONMENT in ["local", "development"] else "4"))
|
||||
|
||||
# Sécurité (production uniquement)
|
||||
SECURE_COOKIES: bool = os.getenv("SECURE_COOKIES", "false").lower() == "true"
|
||||
SECURE_HEADERS: bool = os.getenv("SECURE_HEADERS", "false").lower() == "true"
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""Représentation lisible de la configuration"""
|
||||
return f"""
|
||||
🔧 Configuration LeDiscord - Environnement: {self.ENVIRONMENT.upper()}
|
||||
📊 Debug: {self.DEBUG}
|
||||
🔄 Reload: {self.RELOAD}
|
||||
🌐 CORS Origins: {', '.join(self.CORS_ORIGINS_LIST)}
|
||||
🗄️ Database: {self.DATABASE_URL.split('@')[1] if '@' in self.DATABASE_URL else 'Unknown'}
|
||||
📁 Upload Path: {self.UPLOAD_PATH}
|
||||
📝 Log Level: {self.LOG_LEVEL}
|
||||
🚀 Workers: {self.WORKERS}
|
||||
"""
|
||||
|
||||
def validate(self) -> bool:
|
||||
"""Valide la configuration"""
|
||||
required_vars = [
|
||||
"ENVIRONMENT",
|
||||
"DATABASE_URL",
|
||||
"JWT_SECRET_KEY",
|
||||
"CORS_ORIGINS",
|
||||
"ADMIN_EMAIL",
|
||||
"ADMIN_PASSWORD"
|
||||
]
|
||||
|
||||
for var in required_vars:
|
||||
if not getattr(self, var):
|
||||
print(f"❌ Configuration invalide: {var} est manquant")
|
||||
return False
|
||||
|
||||
print("✅ Configuration validée avec succès")
|
||||
return True
|
||||
|
||||
settings = Settings()
|
||||
# Instance globale
|
||||
try:
|
||||
settings = Settings()
|
||||
except ValueError as e:
|
||||
print(f"❌ Erreur de configuration: {e}")
|
||||
print("💡 Assurez-vous d'utiliser un fichier d'environnement spécifique:")
|
||||
print(" - .env.local pour le développement local")
|
||||
print(" - .env.development pour l'environnement de développement")
|
||||
print(" - .env.production pour la production")
|
||||
raise
|
||||
|
||||
# Validation automatique au démarrage
|
||||
if __name__ == "__main__":
|
||||
print(settings)
|
||||
settings.validate()
|
||||
|
||||
Reference in New Issue
Block a user