Add structured logging throughout the backend: - logging_config.py: Centralized logging configuration with colored output - main.py: Enhanced startup logging showing initialization progress - init_blockchain.py: Detailed blockchain initialization logging - services.py: Election creation logging Logging features: - Emoji prefixes for different log levels (INFO, DEBUG, ERROR, etc.) - Color-coded output for better visibility - Timestamp and module information - Exception stack traces on errors - Separate loggers for different modules This helps debug: - Backend startup sequence - Database initialization - Blockchain election recording - Service operations - Configuration issues
79 lines
2.0 KiB
Python
79 lines
2.0 KiB
Python
"""
|
|
Application FastAPI principale.
|
|
"""
|
|
|
|
import logging
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from .config import settings
|
|
from .database import init_db, get_db
|
|
from .routes import router
|
|
from .init_blockchain import initialize_elections_blockchain
|
|
from .logging_config import setup_logging
|
|
|
|
# Setup logging for the entire application
|
|
setup_logging(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.info("=" * 70)
|
|
logger.info("🚀 Starting E-Voting Backend")
|
|
logger.info("=" * 70)
|
|
|
|
# Initialiser la base de données
|
|
logger.info("📦 Initializing database...")
|
|
try:
|
|
init_db()
|
|
logger.info("✓ Database initialized successfully")
|
|
except Exception as e:
|
|
logger.error(f"✗ Database initialization failed: {e}", exc_info=True)
|
|
raise
|
|
|
|
# Initialiser la blockchain avec les élections existantes
|
|
logger.info("⛓️ Initializing blockchain...")
|
|
try:
|
|
db = next(get_db())
|
|
initialize_elections_blockchain(db)
|
|
db.close()
|
|
logger.info("✓ Blockchain initialization completed")
|
|
except Exception as e:
|
|
logger.error(f"⚠️ Blockchain initialization failed (non-fatal): {e}", exc_info=True)
|
|
|
|
logger.info("=" * 70)
|
|
logger.info("✓ Backend initialization complete, starting FastAPI app")
|
|
logger.info("=" * 70)
|
|
|
|
# Créer l'application FastAPI
|
|
app = FastAPI(
|
|
title=settings.app_name,
|
|
version=settings.app_version,
|
|
debug=settings.debug
|
|
)
|
|
|
|
# Configuration CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # À restreindre en production
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Inclure les routes
|
|
app.include_router(router)
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""Vérifier l'état de l'application"""
|
|
return {"status": "ok", "version": settings.app_version}
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Endpoint root"""
|
|
return {
|
|
"name": settings.app_name,
|
|
"version": settings.app_version,
|
|
"docs": "/docs"
|
|
}
|