Initial commit: 浼佷笟寰俊 AI 鏈哄櫒浜哄姪鐞?MVP
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
68
backend/app/main.py
Normal file
68
backend/app/main.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
from app.routers import auth, wecom
|
||||
from app.routers.admin import sessions, tickets, kb, settings, users
|
||||
from app.logging_config import get_trace_id
|
||||
|
||||
app = FastAPI(title="企微AI助手", version="0.1.0")
|
||||
|
||||
# CORS 必须在最前面
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(StarletteHTTPException)
|
||||
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={"code": exc.status_code, "message": str(exc.detail), "data": None, "trace_id": get_trace_id()},
|
||||
headers={"Access-Control-Allow-Origin": "*"},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||
return JSONResponse(
|
||||
status_code=422,
|
||||
content={"code": 422, "message": "参数校验失败", "data": exc.errors(), "trace_id": get_trace_id()},
|
||||
headers={"Access-Control-Allow-Origin": "*"},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def global_exception_handler(request: Request, exc: Exception):
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"code": 500, "message": str(exc), "data": None, "trace_id": get_trace_id()},
|
||||
headers={"Access-Control-Allow-Origin": "*"},
|
||||
)
|
||||
|
||||
|
||||
app.include_router(auth.router, prefix="/api/auth", tags=["auth"])
|
||||
app.include_router(wecom.router, prefix="/api/wecom", tags=["wecom"])
|
||||
app.include_router(sessions.router, prefix="/api/admin", tags=["admin"])
|
||||
app.include_router(tickets.router, prefix="/api/admin", tags=["admin"])
|
||||
app.include_router(kb.router, prefix="/api/admin", tags=["admin"])
|
||||
app.include_router(settings.router, prefix="/api/admin", tags=["admin"])
|
||||
app.include_router(users.router, prefix="/api/admin", tags=["admin"])
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
def health():
|
||||
return {"status": "up", "service": "backend"}
|
||||
|
||||
|
||||
@app.get("/api/ready")
|
||||
def ready():
|
||||
return {"ready": True, "service": "backend"}
|
||||
Reference in New Issue
Block a user