main.py
4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import asyncio
import os
import sys
# 修复 Windows 下 aiohttp 容易出现的 SSL ConnectionResetError 问题
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# 禁用全局代理,防止 Clash/V2Ray 等代理软件拦截 aiohttp 导致 SSL 握手重置 (ConnectionResetError)
os.environ["NO_PROXY"] = "*"
os.environ["no_proxy"] = "*"
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
# 引入核心组件
from src.utils.logger import file_handler
from src.database.session import init_db
from src.database.schema_sync import ensure_wiki_schema
# 引入模块化路由
from src.api.routes.files import router as files_router
from src.api.routes.ingest import router as ingest_router
from src.api.routes.query import router as query_router
from src.api.routes.wiki import router as wiki_router
from src.api.routes.lint import router as lint_router
from src.api.routes.multi_path_search import router as multi_path_router
from src.api.routes.auth import router as auth_router
from src.api.routes.admin import router as admin_router
from src.api.routes.sessions import router as sessions_router
from src.api.routes.profile import router as profile_router
from src.rag.api.routes import router as rag_router
app = FastAPI(
title="LLM Wiki API",
description="Agentic Backend for Automated Knowledge Base Generation",
version="1.3.0"
)
# CORS 配置
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.on_event("startup")
async def startup_event():
import logging
logger = logging.getLogger("fastapi")
logger.info("Initializing database...")
await init_db()
await ensure_wiki_schema()
logger.info("Database initialized.")
# 确保预置管理员账号存在(幂等:已存在则不覆盖其密码)
try:
from src.services.identity.seed_admin import ensure_admin_user
created = await ensure_admin_user()
logger.info("Admin seed check finished: created=%s", created)
except Exception as exc:
logger.warning("Admin seed check failed: %s", exc)
# #3: 重启恢复 — 将上次中断的 Ingesting 文件回滚为 Pending
try:
from src.api.routes.ingest import recover_interrupted_ingests
recovered = await recover_interrupted_ingests()
logger.info("Ingest recovery check finished: recovered=%d", recovered)
except Exception as exc:
logger.warning("Ingest recovery check failed: %s", exc)
try:
from src.rag.service import repair_interrupted_indexes
result = await repair_interrupted_indexes(cleanup_partial=False)
logger.info("RAG recovery check finished: %s", result)
except Exception as exc:
logger.warning("RAG recovery check skipped or failed: %s", exc)
# 长期记忆沉淀的后台调度(REFLECT_TRIGGER_MODE=off 时不注册任何任务)
try:
from src.workers.memory_worker import start_memory_worker
start_memory_worker()
except Exception as exc:
logger.warning("Memory worker start failed: %s", exc)
@app.on_event("shutdown")
async def shutdown_event():
import logging
logger = logging.getLogger("fastapi")
logger.info("Shutting down Application...")
try:
from src.workers.memory_worker import stop_memory_worker
stop_memory_worker()
except Exception as exc:
logger.warning("Memory worker stop failed: %s", exc)
if file_handler:
file_handler.close()
# 挂载路由
app.include_router(files_router)
app.include_router(ingest_router)
app.include_router(query_router)
app.include_router(wiki_router)
app.include_router(lint_router)
app.include_router(multi_path_router)
app.include_router(auth_router)
app.include_router(admin_router)
app.include_router(sessions_router)
app.include_router(profile_router)
app.include_router(rag_router)
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8104, reload=False)