File size: 1,112 Bytes
e109700
 
c35fb5d
e109700
 
 
 
 
 
 
 
 
 
 
 
ba5d6d2
 
 
 
 
 
 
 
e109700
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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"])