zhaoqingang
2025-03-03 f95f801f35aa201cbaffd7d881c07edc9398b570
main.py
@@ -1,5 +1,5 @@
from contextlib import asynccontextmanager
# from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.background import BackgroundScheduler
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
@@ -12,8 +12,11 @@
from app.api.excel import router as excel_router
from app.api.files import router as files_router
from app.api.knowledge import knowledge_router
from app.api.label import label_router
from app.api.llm import llm_router
from app.api.organization import dept_router
from app.api.system import system_router
from app.api.v2.chat import chat_router_v2
from app.api.v2.public_api import public_api
from app.api.report import router as report_router
from app.api.resource import menu_router
@@ -21,23 +24,35 @@
from app.api.user import user_router
from app.api.group import group_router
from app.api.role import role_router
from app.task.fetch_agent import sync_agents, initialize_agents, sync_web_menu, sync_default_data
from app.models.base_model import init_db
from app.task.delete_execl_file import delete_file_after_delay
# from app.models.base_model import init_db
from app.task.fetch_agent import sync_agents, initialize_agents, sync_agents_v2, sync_knowledge, \
    sync_resources_from_json
from app.init_config.init_run_data import sync_default_data
from app.task.sync_account_token import start_sync_token_task, sync_token
init_db()
# 使用 Lifespan 事件处理程序
@asynccontextmanager
async def lifespan(app: FastAPI):
    # 初始化代理
    initialize_agents()
    # 在应用启动时同步代理
    sync_agents()
    await sync_default_data()
    await sync_web_menu()
    # initialize_agents()
    # # 在应用启动时同步代理
    # sync_agents()
    # await sync_default_data()  # Todo
    sync_agents_v2() # 智能体
    sync_knowledge() # 知识库
    sync_resources_from_json()
    yield
    # 在应用关闭时执行清理操作(如果需要)
    pass
# init_db()
app = FastAPI(
    title="basic_rag_gateway",
    version="0.1",
@@ -46,18 +61,19 @@
)
# 设置 CORS 中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有域名跨域访问
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有方法
    allow_headers=["*"],  # 允许所有头部
)
# app.add_middleware(
#     CORSMiddleware,
#     allow_origins=["*"],
#     allow_credentials=True,
#     allow_methods=["*"],  # 允许所有方法
#     allow_headers=["*"],  # 允许所有头部
# )
# 创建调度器
# scheduler = BackgroundScheduler()
# scheduler.add_job(sync_resource, 'interval', minutes=1, id="sync_resource_data")
# scheduler.start()
scheduler = BackgroundScheduler()
scheduler.add_job(sync_agents_v2, 'interval', minutes=60, id="sync_resource_data")
scheduler.add_job(start_sync_token_task, 'interval', minutes=5, id="sync_token_1")
# scheduler.add_job(delete_file_after_delay, 'interval', minutes=10, id="delete_file_after_delay")
scheduler.start()
app.include_router(auth_router, prefix='/api/auth', tags=["auth"])
app.include_router(chat_router, prefix='/api/chat', tags=["chat"])
@@ -74,10 +90,13 @@
app.include_router(llm_router, prefix='/api/llm', tags=["llm"])
app.include_router(dialog_router, prefix='/api/dialog', tags=["dialog"])
app.include_router(canvas_router, prefix='/api/canvas', tags=["canvas"])
# app.include_router(sync_router, prefix='/api/sync', tags=["sync"])
app.include_router(label_router, prefix='/api/label', tags=["label"])
app.include_router(public_api, prefix='/v1/api', tags=["public_api"])
app.include_router(chat_router_v2, prefix='/api/v1', tags=["chat1"])
app.include_router(system_router, prefix='/api/system', tags=["system"])
app.mount("/static", StaticFiles(directory="app/images"), name="static")
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=9201)