86 lines
2.6 KiB
Python
86 lines
2.6 KiB
Python
"""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(
|
|
title=get_settings().APP_NAME,
|
|
version=get_settings().APP_VERSION,
|
|
description="Platform - Module Management System",
|
|
openapi_url="/openapi.json",
|
|
)
|
|
|
|
# CORS
|
|
cors_origins = get_settings().APP_CORS_ORIGINS.split(",")
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=cors_origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 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")
|
|
app.include_router(roles.router, prefix="/api/v1")
|
|
app.include_router(modules.router, prefix="/api/v1")
|
|
app.include_router(platform_modules.router, prefix="/api/v1")
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup():
|
|
init_db()
|
|
|
|
# Load and register modules
|
|
try:
|
|
registered = load_modules()
|
|
logger.info(f"Modules loaded: {registered}")
|
|
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()
|
|
except Exception as e:
|
|
logger.error(f"Module startup error: {e}")
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
async def shutdown():
|
|
try:
|
|
await shutdown_modules()
|
|
except Exception as e:
|
|
logger.error(f"Module shutdown error: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run("app.main:app", host="0.0.0.0", port=9000, reload=True)
|