Spaces:
Runtime error
Runtime error
Download app/main.py from dedalys-civ/dedalys-api: direct link, hf CLI and curl.
- Browser
- Download file 6.15 kB
-
https://huggingface.co/spaces/dedalys-civ/dedalys-api/resolve/main/app/main.py
- Command line
-
hf download hf://spaces/dedalys-civ/dedalys-api/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/dedalys-civ/dedalys-api/resolve/main/app/main.py
6.15 kB
| """ | |
| Dedalys Backend — Point d'entrée FastAPI | |
| Solution de gestion pour cabinets et directions juridiques. | |
| """ | |
| import time | |
| import logging | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from app.core.config import settings | |
| from app.core.scheduler import start_scheduler, stop_scheduler | |
| from app.routers import ( | |
| auth, | |
| espaces, | |
| utilisateurs, | |
| clients, | |
| dossiers, | |
| audiences, | |
| flash_cr, | |
| paiements, | |
| fichiers, | |
| audit, | |
| ) | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| # LIFESPAN (Démarrage / Arrêt) | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| async def lifespan(app: FastAPI): | |
| # Démarrage | |
| start_scheduler() | |
| yield | |
| # Arrêt | |
| stop_scheduler() | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| # APPLICATION | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| app = FastAPI( | |
| title="Dedalys API", | |
| description="API pour la gestion des cabinets d'avocats et directions juridiques en Afrique francophone.", | |
| version="1.0.0", | |
| docs_url="/api/v1/docs" if settings.is_development else None, | |
| redoc_url="/api/v1/redoc" if settings.is_development else None, | |
| lifespan=lifespan, | |
| ) | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| # MIDDLEWARE CORS | |
| # SECURITY FIX M1: Restreindre les méthodes et headers autorisés. | |
| # allow_methods=["*"] et allow_headers=["*"] ouvrent la porte à des requêtes | |
| # cross-origin non désirées (DELETE depuis n'importe quel origin, etc.) | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=settings.cors_origins, | |
| allow_credentials=True, | |
| allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], | |
| allow_headers=["Content-Type", "Authorization", "X-Requested-With"], | |
| max_age=3600, | |
| ) | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| # MIDDLEWARE LOGGING | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| async def log_requests(request: Request, call_next): | |
| start_time = time.time() | |
| # Process request | |
| response = await call_next(request) | |
| process_time = time.time() - start_time | |
| log_message = ( | |
| f"{request.method} || " | |
| f"{request.url.path} || " | |
| f"{response.status_code} || " | |
| f"{process_time:.4f}s" | |
| ) | |
| logger.info(log_message) | |
| return response | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| # ROUTERS — TOUS SOUS /api/v1 | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| api_prefix = "/api/v1" | |
| app.include_router(auth.router, prefix=api_prefix) | |
| app.include_router(espaces.router, prefix=api_prefix) | |
| app.include_router(utilisateurs.router, prefix=api_prefix) | |
| app.include_router(clients.router, prefix=api_prefix) | |
| app.include_router(dossiers.router, prefix=api_prefix) | |
| app.include_router(audiences.router, prefix=api_prefix) | |
| app.include_router(flash_cr.router, prefix=api_prefix) | |
| app.include_router(paiements.router, prefix=api_prefix) | |
| app.include_router(fichiers.router, prefix=api_prefix) | |
| app.include_router(audit.router, prefix=api_prefix) | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| # HEALTH CHECK | |
| # ══════════════════════════════════════════════════════════════════════════════ | |
| async def health_check(): | |
| """Endpoint de vérification de l'état de l'API.""" | |
| return { | |
| "status": "ok", | |
| "service": "Dedalys API", | |
| "version": "1.0.0", | |
| "environment": settings.APP_ENV, | |
| } | |
| async def root(): | |
| """Redirection vers la documentation.""" | |
| return { | |
| "message": "Bienvenue sur l'API Dedalys", | |
| "documentation": "/api/v1/docs", | |
| "health": "/api/v1/health", | |
| } | |