from fastapi import APIRouter, Request, HTTPException import logging import os from api import prediction from config.settings import API_KEY logger = logging.getLogger(__name__) # Router principal router = APIRouter() # Middleware d'authentification async def verify_api_key(request: Request, call_next): """Middleware pour vérifier la clé API dans les en-têtes.""" # Skip if we're in debug mode or during startup if os.environ.get("DEBUG") == "1": return await call_next(request) # Pour les routes Hugging Face Space (logs, etc.) if request.url.path == "/" and "logs=container" in request.url.query: return await call_next(request) api_key = request.headers.get("x-api-key") if api_key is None or api_key not in API_KEY.split(','): logger.warning(f"Unauthorized API access attempt from {request.client.host}") raise HTTPException(status_code=403, detail="Unauthorized") response = await call_next(request) return response # Inclure les routes des autres modules router.include_router(prediction.router, tags=["Prediction"])