Files
000-platform/backend/app/main.py
Admin afcf30b519
All checks were successful
continuous-integration/drone/push Build is passing
feat: 新增睿美云对接模块
- 扩展 ToolConfig 配置类型,新增 external_api 类型
- 实现接口注册表,包含 90+ 睿美云开放接口定义
- 实现 TPOS SHA256WithRSA 签名鉴权
- 实现睿美云 API 客户端,支持多租户配置
- 新增代理路由 /api/ruimeiyun/call/{api_name}
- 支持接口权限控制和健康检查
2026-01-30 17:27:58 +08:00

100 lines
3.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""平台服务入口"""
import logging
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .config import get_settings
from .routers import stats_router, logs_router, config_router, health_router
from .routers.auth import router as auth_router
from .routers.tenants import router as tenants_router
from .routers.tenant_apps import router as tenant_apps_router
from .routers.tenant_wechat_apps import router as tenant_wechat_apps_router
from .routers.apps import router as apps_router
from .routers.wechat import router as wechat_router
from .routers.alerts import router as alerts_router
from .routers.cost import router as cost_router
from .routers.quota import router as quota_router
from .routers.tasks import router as tasks_router
from .routers.notification_channels import router as notification_channels_router
from .routers.tool_configs import router as tool_configs_router
from .routers.ruimeiyun import router as ruimeiyun_router
from .middleware import TraceMiddleware, setup_exception_handlers, RequestLoggerMiddleware
from .middleware.trace import setup_logging
from .services.scheduler import scheduler_service
# 配置日志(包含 TraceID
setup_logging(level=logging.INFO, include_trace=True)
settings = get_settings()
app = FastAPI(
title=settings.APP_NAME,
version=settings.APP_VERSION,
description="平台基础设施服务 - 统计/日志/配置管理"
)
# 配置统一异常处理
setup_exception_handlers(app)
# 中间件按添加的反序执行,所以:
# 1. CORS 最后添加,最先执行
# 2. TraceMiddleware 在 RequestLoggerMiddleware 之后添加,这样先执行
# 3. RequestLoggerMiddleware 最先添加,最后执行(此时 trace_id 已设置)
# 请求日志中间件(自动记录到数据库)
app.add_middleware(RequestLoggerMiddleware, app_code="000-platform")
# TraceID 追踪中间件
app.add_middleware(TraceMiddleware, log_requests=True)
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["X-Trace-ID", "X-Response-Time"]
)
# 注册路由
app.include_router(health_router, prefix="/api")
app.include_router(auth_router, prefix="/api")
app.include_router(tenants_router, prefix="/api")
app.include_router(tenant_apps_router, prefix="/api")
app.include_router(tenant_wechat_apps_router, prefix="/api")
app.include_router(apps_router, prefix="/api")
app.include_router(stats_router, prefix="/api")
app.include_router(logs_router, prefix="/api")
app.include_router(config_router, prefix="/api")
app.include_router(wechat_router, prefix="/api")
app.include_router(alerts_router, prefix="/api")
app.include_router(cost_router, prefix="/api")
app.include_router(quota_router, prefix="/api")
app.include_router(tasks_router)
app.include_router(notification_channels_router)
app.include_router(tool_configs_router, prefix="/api")
app.include_router(ruimeiyun_router, prefix="/api")
# 应用生命周期事件
@app.on_event("startup")
async def startup_event():
"""应用启动时启动调度器"""
scheduler_service.start()
@app.on_event("shutdown")
async def shutdown_event():
"""应用关闭时关闭调度器"""
scheduler_service.shutdown()
@app.get("/")
async def root():
return {
"service": settings.APP_NAME,
"version": settings.APP_VERSION,
"docs": "/docs"
}