77 lines
No EOL
2.3 KiB
Python
77 lines
No EOL
2.3 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.websockets import WebSocket
|
|
import uvicorn
|
|
import logging
|
|
import asyncio
|
|
from app.routes import containers, agents
|
|
from app.services.agent import AgentService
|
|
|
|
# Configuration du logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
app = FastAPI(
|
|
title="Étoile Polaire",
|
|
description="API de gestion des conteneurs Docker et des agents",
|
|
version="1.0.0"
|
|
)
|
|
|
|
# Configuration CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["http://localhost:3000"], # Frontend URL
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Inclusion des routes
|
|
app.include_router(containers.router)
|
|
app.include_router(agents.router)
|
|
|
|
# Service d'agents
|
|
agent_service = AgentService()
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "Bienvenue sur l'API Étoile Polaire"}
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
return {"status": "healthy"}
|
|
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
"""Tâches à exécuter au démarrage de l'application."""
|
|
# Démarrer la tâche de nettoyage des agents inactifs
|
|
asyncio.create_task(cleanup_inactive_agents())
|
|
|
|
async def cleanup_inactive_agents():
|
|
"""Nettoie périodiquement les agents inactifs."""
|
|
while True:
|
|
try:
|
|
inactive_agents = await agent_service.cleanup_inactive_agents()
|
|
if inactive_agents:
|
|
logger.info(f"Agents inactifs supprimés : {inactive_agents}")
|
|
except Exception as e:
|
|
logger.error(f"Erreur lors du nettoyage des agents : {e}")
|
|
await asyncio.sleep(300) # Vérifier toutes les 5 minutes
|
|
|
|
# WebSocket pour les logs en temps réel
|
|
@app.websocket("/ws/logs")
|
|
async def websocket_endpoint(websocket: WebSocket):
|
|
await websocket.accept()
|
|
try:
|
|
while True:
|
|
# TODO: Implémenter la logique de streaming des logs
|
|
data = await websocket.receive_text()
|
|
await websocket.send_text(f"Message reçu: {data}")
|
|
except Exception as e:
|
|
logger.error(f"Erreur WebSocket: {e}")
|
|
finally:
|
|
await websocket.close()
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |