⚒️ 重大重构 LoveACE V2

引入了 mongodb
对数据库进行了一定程度的数据加密
性能改善
代码简化
统一错误模型和响应
使用 apifox 作为文档
This commit is contained in:
2025-11-20 20:44:25 +08:00
parent 6b90c6d7bb
commit bbc86b8330
168 changed files with 14264 additions and 19152 deletions

161
main.py
View File

@@ -1,25 +1,29 @@
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI
from database.creator import db_manager
from router.invite import invite_router
from router.jwc import jwc_router
from router.login import login_router
from router.aac import aac_router
from router.user import user_router
from router.isim import isim_router
from richuru import install
from fastapi.middleware.cors import CORSMiddleware as allow_origins
import uvicorn
# 导入配置管理器和日志设置
from config import config_manager
from config.logger import setup_logger, get_logger
# 初始化日志系统
install()
setup_logger()
logger = get_logger()
import uvicorn
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException
from loveace.config.logger import logger
from loveace.config.manager import config_manager
from loveace.database.creator import db_manager
from loveace.middleware.process_time import ProcessTimeMiddleware
from loveace.router.endpoint.aac import aac_base_router
from loveace.router.endpoint.ldjlb import ldjlb_base_router
from loveace.router.endpoint.apifox import apifox_router
from loveace.router.endpoint.auth import auth_router
from loveace.router.endpoint.isim import isim_base_router
from loveace.router.endpoint.jwc import jwc_base_router
from loveace.router.endpoint.profile import profile_router
from loveace.router.endpoint.utils.alive import alive_router
from loveace.router.schemas.exception import UniResponseHTTPException
from loveace.router.schemas.model import ValidationErrorDetail, ValidationErrorModel
from loveace.router.schemas.uniresponse import UniResponseModel
from loveace.service.remote.aufe.depends import service as aufe_service
@asynccontextmanager
@@ -28,54 +32,135 @@ async def lifespan(app: FastAPI):
if not config_manager.validate_config():
logger.error("配置文件验证失败,请检查配置")
raise RuntimeError("配置文件验证失败")
logger.info("应用程序启动中...")
# 启动时连接数据库
await db_manager.init_db()
logger.success("数据库连接成功")
if await db_manager.init_db():
logger.info("数据库连接成功")
else:
logger.error("数据库连接失败,应用程序无法启动")
await asyncio.sleep(5)
raise RuntimeError("数据库初始化失败")
# 启动时连接Redis
try:
await db_manager.get_redis_client()
logger.info("Redis连接成功")
except Exception as e:
logger.error(f"Redis连接失败: {e}")
await db_manager.close_db()
raise RuntimeError("Redis初始化失败")
# 启动时执行 AUFE 服务初始化
try:
await aufe_service.initialize()
logger.info("AUFE服务初始化成功")
except Exception as e:
logger.error(f"AUFE服务初始化失败: {e}")
raise
yield
# 关闭时断开Redis连接
await db_manager.close_redis()
logger.info("Redis连接已关闭")
# 关闭时断开数据库连接
await db_manager.close_db()
logger.info("应用程序已关闭")
# 关闭时清理 AUFE 服务
try:
await aufe_service.shutdown()
except Exception as e:
logger.warning(f"AUFE服务关闭异常: {e}")
# 获取应用配置
app_config = config_manager.get_settings().app
# Production FastAPI application
# 创建FastAPI应用
app = FastAPI(
lifespan=lifespan,
title=app_config.title,
description=app_config.description,
version=app_config.version,
debug=app_config.debug,
docs_url=None if not app_config.debug else "/docs",
redoc_url=None if not app_config.debug else "/redoc",
openapi_url=None if not app_config.debug else "/openapi.json",
docs_url="/docs" if app_config.debug else None,
redoc_url="/redoc" if app_config.debug else None,
openapi_url="/openapi.json" if app_config.debug else None,
)
# CORS配置
app.add_middleware(
allow_origins,
CORSMiddleware,
allow_origins=app_config.cors_allow_origins,
allow_credentials=app_config.cors_allow_credentials,
allow_methods=app_config.cors_allow_methods,
allow_headers=app_config.cors_allow_headers,
)
@app.get("/")
async def root():
return {"message": "Hello World"}
# 处理时间中间件
app.add_middleware(ProcessTimeMiddleware)
# 注册路由
app.include_router(apifox_router)
app.include_router(profile_router)
app.include_router(alive_router)
app.include_router(auth_router)
app.include_router(jwc_base_router)
app.include_router(aac_base_router)
app.include_router(ldjlb_base_router)
app.include_router(isim_base_router)
async def uniresponse_http_exception_handler(
request: Request, exc: UniResponseHTTPException
):
return JSONResponse(
status_code=exc.status_code,
content=exc.uni_response.model_dump(),
)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
status_code=422,
content=UniResponseModel(
success=False,
data=None,
message=None,
error=ValidationErrorModel(
message="请求参数验证错误",
code="VALIDATION_ERROR",
trace_id="",
details=[
ValidationErrorDetail(
loc=err["loc"],
msg=err["msg"],
type=err["type"],
)
for err in exc.errors()
],
),
).model_dump(),
)
app.exception_handlers[UniResponseHTTPException] = uniresponse_http_exception_handler
app.exception_handlers[RequestValidationError] = validation_exception_handler
app.include_router(invite_router)
app.include_router(jwc_router)
app.include_router(login_router)
app.include_router(aac_router)
app.include_router(user_router)
app.include_router(isim_router)
if __name__ == "__main__":
uvicorn.run(app, host=app_config.host, port=app_config.port)
if app_config.debug:
uvicorn.run(
app,
host=app_config.host,
port=app_config.port,
workers=app_config.workers,
)
else:
logger.info(
f"请手动输入如下指令启动服务:\ngranian --interface asgi main:app --host {app_config.host} --port {app_config.port} --workers {app_config.workers} --process-name LoveACE-V2"
)