68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
|
|
from fastapi import FastAPI
|
|||
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|||
|
|
from app.api.routes import api_router
|
|||
|
|
from app.core.config import settings
|
|||
|
|
from app.utils.logger import logger
|
|||
|
|
from app.services import milvus_service
|
|||
|
|
|
|||
|
|
# 创建应用
|
|||
|
|
app = FastAPI(
|
|||
|
|
title="车辆法规智能检索系统",
|
|||
|
|
description="基于RAG技术的法规检索与合规分析后端API",
|
|||
|
|
version="1.0.0",
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# CORS配置
|
|||
|
|
app.add_middleware(
|
|||
|
|
CORSMiddleware,
|
|||
|
|
allow_origins=["*"],
|
|||
|
|
allow_credentials=True,
|
|||
|
|
allow_methods=["*"],
|
|||
|
|
allow_headers=["*"],
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 注册路由
|
|||
|
|
app.include_router(api_router, prefix="/api")
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.on_event("startup")
|
|||
|
|
async def startup_event():
|
|||
|
|
"""启动时初始化"""
|
|||
|
|
logger.info("Starting application...")
|
|||
|
|
|
|||
|
|
# 初始化Milvus集合(仅在服务可用时)
|
|||
|
|
try:
|
|||
|
|
if milvus_service is not None:
|
|||
|
|
milvus_service.create_regulations_collection()
|
|||
|
|
logger.info("Milvus collection initialized")
|
|||
|
|
else:
|
|||
|
|
logger.warning("Milvus service not available, using mock data")
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.warning(f"Milvus initialization failed: {e}")
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.on_event("shutdown")
|
|||
|
|
async def shutdown_event():
|
|||
|
|
"""关闭时清理"""
|
|||
|
|
logger.info("Shutting down application...")
|
|||
|
|
try:
|
|||
|
|
if milvus_service is not None:
|
|||
|
|
milvus_service.disconnect()
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.warning(f"Shutdown cleanup error: {e}")
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.get("/")
|
|||
|
|
async def root():
|
|||
|
|
"""根路径"""
|
|||
|
|
return {
|
|||
|
|
"message": "车辆法规智能检索系统 API",
|
|||
|
|
"version": "1.0.0",
|
|||
|
|
"docs": "/docs",
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.get("/health")
|
|||
|
|
async def health():
|
|||
|
|
"""健康检查"""
|
|||
|
|
return {"status": "healthy"}
|