TT-002: modular router registration and module infrastructure
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
"""Platform - Main Application."""
|
||||
|
||||
from pathlib import Path
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from app.settings import get_settings
|
||||
from app.core.database import init_db
|
||||
from app.api.v1 import auth, users, roles, modules, health
|
||||
from app.api.v1 import platform_modules
|
||||
from app.core.module_loader import load_modules, startup_modules, shutdown_modules
|
||||
from app.core.module_registry import get_module_registry
|
||||
from app.core.logging import logger
|
||||
|
||||
app = FastAPI(
|
||||
@@ -27,7 +30,7 @@ app.add_middleware(
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# Routes
|
||||
# Platform Routes
|
||||
app.include_router(health.router, prefix="/api/v1")
|
||||
app.include_router(auth.router, prefix="/api/v1")
|
||||
app.include_router(users.router, prefix="/api/v1")
|
||||
@@ -47,6 +50,21 @@ async def startup():
|
||||
except Exception as e:
|
||||
logger.error(f"Module loading error: {e}")
|
||||
|
||||
# Auto-connect module routers and static files
|
||||
registry = get_module_registry()
|
||||
for module in registry.all():
|
||||
# Mount routers
|
||||
for router in module.routers():
|
||||
app.include_router(router)
|
||||
logger.info(f"Mounted router from module '{module.id}'")
|
||||
|
||||
# Mount static files
|
||||
static = module.static_path()
|
||||
if static and Path(static).exists():
|
||||
mount_path = f"/modules/{module.id}/static"
|
||||
app.mount(mount_path, StaticFiles(directory=str(static)), name=f"{module.id}-static")
|
||||
logger.info(f"Mounted static for module '{module.id}' at {mount_path}")
|
||||
|
||||
# Call module startup hooks
|
||||
try:
|
||||
await startup_modules()
|
||||
@@ -56,7 +74,6 @@ async def startup():
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown():
|
||||
# Call module shutdown hooks
|
||||
try:
|
||||
await shutdown_modules()
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user