hyf-backend/main.py

140 lines
4.8 KiB
Python
Raw Normal View History

2026-01-21 13:45:39 +08:00
# uvicorn main:app --host 0.0.0.0 --port 8000 --reload
# 1. pip install fastapi-cdn-host
# 2. import fastapi_cdn_host
# 3. fastapi_cdn_host.patch_docs(app)
from fastapi import FastAPI
import fastapi_cdn_host
from os.path import dirname, realpath
from dotenv import load_dotenv
load_dotenv()
from utils.util_log import init_logger
from loguru import logger
base_dir: str = dirname(realpath(__file__))
init_logger(base_dir)
from th_agenter.api.routes import router
from contextlib import asynccontextmanager
from starlette.exceptions import HTTPException as StarletteHTTPException
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan manager."""
logger.info("[生命周期] - Starting up TH Agenter application...")
yield
# Shutdown
logger.info("[生命周期] - Shutting down TH Agenter application...")
def setup_exception_handlers(app: FastAPI) -> None:
"""Setup global exception handlers."""
# Import custom exceptions and handlers
from utils.util_exceptions import ChatAgentException, chat_agent_exception_handler
@app.exception_handler(ChatAgentException)
async def custom_chat_agent_exception_handler(request, exc):
return await chat_agent_exception_handler(request, exc)
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request, exc):
from utils.util_exceptions import HxfErrorResponse
logger.exception(f"HTTP Exception: {exc.status_code} - {exc.detail} - {request.method} {request.url}")
return HxfErrorResponse(exc)
def make_json_serializable(obj):
"""递归地将对象转换为JSON可序列化的格式"""
if obj is None or isinstance(obj, (str, int, float, bool)):
return obj
elif isinstance(obj, bytes):
return obj.decode('utf-8')
elif isinstance(obj, (ValueError, Exception)):
return str(obj)
elif isinstance(obj, dict):
return {k: make_json_serializable(v) for k, v in obj.items()}
elif isinstance(obj, (list, tuple)):
return [make_json_serializable(item) for item in obj]
else:
# For any other object, convert to string
return str(obj)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
# Convert any non-serializable objects to strings in error details
try:
errors = make_json_serializable(exc.errors())
except Exception as e:
# Fallback: if even our conversion fails, use a simple error message
errors = [{"type": "serialization_error", "msg": f"Error processing validation details: {str(e)}"}]
logger.exception(f"Request Validation Error: {errors}")
logger.exception(f"validation_error: {errors}")
return JSONResponse(
status_code=422,
content={
"error": {
"type": "validation_error",
"message": "Request validation failed",
"details": errors
}
}
)
@app.exception_handler(Exception)
async def general_exception_handler(request, exc):
logger.error(f"Unhandled exception: {exc}", exc_info=True)
return JSONResponse(
status_code=500,
content={
"error": {
"type": "internal_error",
"message": "Internal server error"
}
}
)
def create_app() -> FastAPI:
"""Create and configure FastAPI application."""
from th_agenter.core.config import get_settings
settings = get_settings()
# Create FastAPI app
app = FastAPI(
title=settings.app_name,
version=settings.app_version,
description="基于Vue的第一个聊天智能体应用使用FastAPI后端由TH Agenter修改",
debug=settings.debug,
lifespan=lifespan,
)
# Add middleware
from th_agenter.core.app import setup_middleware
setup_middleware(app, settings)
# # Add exception handlers
setup_exception_handlers(app)
add_router(app)
return app
def add_router(app: FastAPI) -> None:
"""Add default routers to the FastAPI application."""
@app.get("/")
def read_root():
logger.info("Hello World")
return {"Hello": "World"}
# Include routers
app.include_router(router, prefix="/api")
app = create_app()
fastapi_cdn_host.patch_docs(app)
# from test.example import internet_search_tool