69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from uvicorn.server import logger
|
|
|
|
|
|
async def test_init():
|
|
from service.sync.department import sync_department, check_department_datebase
|
|
from service.sync.employee import sync_department_user, check_employee_datebase
|
|
|
|
if not check_department_datebase():
|
|
logger.info("[数据库] 开始同步部门 📦")
|
|
await sync_department()
|
|
logger.info("[数据库] 同步部门完成 📦")
|
|
if not check_employee_datebase():
|
|
logger.info("[数据库] 开始同步员工 📦")
|
|
await sync_department_user()
|
|
logger.info("[数据库] 同步员工完成 📦")
|
|
|
|
|
|
def init_database():
|
|
from model import create_db_and_tables
|
|
|
|
logger.info("[数据库] 初始化数据库 📦")
|
|
create_db_and_tables()
|
|
logger.info("[数据库] 数据库初始化完成 ✅")
|
|
|
|
|
|
def init_scheduler(app: FastAPI):
|
|
from scheduler import init_scheduler_router
|
|
|
|
logger.info("[定时任务] 初始化定时任务 📦")
|
|
init_scheduler_router(app)
|
|
logger.info("[定时任务] 定时任务初始化完成 ✅")
|
|
|
|
|
|
def active_config():
|
|
logger.info(f"[激活配置] 加载配置 ⚙️")
|
|
from config import Settings # noqa
|
|
|
|
|
|
def import_router(app: FastAPI):
|
|
logger.info(f"[导入路由] 开始导入路由 🛣️")
|
|
from router import router
|
|
|
|
app.include_router(router)
|
|
logger.info(f"[导入路由] 路由导入完成 ✅")
|
|
|
|
|
|
async def import_mcp_server(app: FastAPI):
|
|
logger.info(f"[导入MCP] 开始导入MCP 🛣️")
|
|
from mcps import create_mcp_app
|
|
|
|
app.mount("/app", await create_mcp_app())
|
|
logger.info(f"[导入MCP] MCP导入完成 ✅")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
logger.info(f"[生命周期] 应用启动 🚀")
|
|
active_config()
|
|
init_database()
|
|
import_router(app)
|
|
init_scheduler(app)
|
|
await import_mcp_server(app)
|
|
await test_init()
|
|
yield
|
|
logger.info(f"[生命周期] 应用关闭 🔧✅")
|