58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
"""Application FastAPI principale avec auto-chargement des contrôleurs"""
|
|
from fastapi import FastAPI
|
|
import uvicorn
|
|
from pathlib import Path
|
|
import importlib
|
|
import os
|
|
from config import settings
|
|
|
|
# Créer l'application FastAPI
|
|
app = FastAPI(
|
|
title="Nextcloud API",
|
|
description="API pour lister les fichiers et dossiers d'un serveur Nextcloud",
|
|
version="1.0.0"
|
|
)
|
|
|
|
|
|
def auto_load_controllers():
|
|
"""Charge automatiquement tous les contrôleurs depuis le dossier controllers"""
|
|
controllers_dir = Path(__file__).parent / "controllers"
|
|
|
|
if not controllers_dir.exists():
|
|
print("⚠️ Le dossier 'controllers' n'existe pas")
|
|
return
|
|
|
|
# Parcourir tous les fichiers Python dans le dossier controllers
|
|
for file_path in controllers_dir.glob("*.py"):
|
|
# Ignorer __init__.py
|
|
if file_path.stem == "__init__":
|
|
continue
|
|
|
|
try:
|
|
# Importer le module dynamiquement
|
|
module_name = f"controllers.{file_path.stem}"
|
|
module = importlib.import_module(module_name)
|
|
|
|
# Vérifier si le module a un attribut 'router'
|
|
if hasattr(module, "router"):
|
|
app.include_router(module.router)
|
|
print(f"✅ Contrôleur chargé : {file_path.stem}")
|
|
else:
|
|
print(f"⚠️ Le fichier {file_path.stem}.py n'a pas de 'router'")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Erreur lors du chargement de {file_path.stem}: {str(e)}")
|
|
|
|
|
|
# Auto-charger tous les contrôleurs au démarrage
|
|
auto_load_controllers()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(
|
|
"main:app",
|
|
host=settings.app_host,
|
|
port=settings.app_port,
|
|
reload=True
|
|
)
|