#!/usr/bin/env python """ Script pour démarrer l'API TIM. Usage: python scripts/start_api.py [--port PORT] """ import socket import sys import uvicorn def find_free_port(start_port=8001, max_attempts=10): """Trouve un port libre à partir de start_port.""" for port in range(start_port, start_port + max_attempts): try: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(('', port)) return port except OSError: continue raise RuntimeError(f"Aucun port libre trouvé entre {start_port} et {start_port + max_attempts}") if __name__ == "__main__": # Vérifier si un port est spécifié en argument port = None if len(sys.argv) > 1: if sys.argv[1] == "--port" and len(sys.argv) > 2: try: port = int(sys.argv[2]) except ValueError: print(f"❌ Port invalide: {sys.argv[2]}") sys.exit(1) # Trouver un port libre si non spécifié if port is None: port = find_free_port() print("🚀 Démarrage de l'API TIM...") print(f"📍 Interface web disponible sur: http://localhost:{port}") print(f"📚 Documentation API disponible sur: http://localhost:{port}/docs") print(f"📖 Documentation alternative sur: http://localhost:{port}/redoc") print("\nAppuyez sur Ctrl+C pour arrêter le serveur\n") uvicorn.run( "pipeline_mco_pmsi.api.tim_api:app", host="0.0.0.0", port=port, reload=True, # Rechargement automatique en développement log_level="info", )