import uvicorn
from fastapi import FastAPI

from ds.config import SERVER_CONFIG
from middle.error_handler import global_exception_handler
from service.user_service import router as user_router
from service.device_service import router as device_router
from ws.ws import ws_router, lifespan

# ------------------------------
# 初始化 FastAPI 应用、指定生命周期管理
# ------------------------------
app = FastAPI(
    title="内容安全审核后台",
    description="内容安全审核后台",
    version="1.0.0",
    lifespan=lifespan
)

# ------------------------------
# 注册路由
# ------------------------------
app.include_router(user_router)
app.include_router(device_router)
app.include_router(ws_router)

# ------------------------------
# 注册全局异常处理器
# ------------------------------
app.add_exception_handler(Exception, global_exception_handler)

# ------------------------------
# 启动服务
# ------------------------------
if __name__ == "__main__":
    port = int(SERVER_CONFIG.get("port", 8000))
    uvicorn.run(
        app="main:app",
        host="0.0.0.0",
        port=port,
        reload=True,
        ws="websockets"
    )
