3cc35c0336
- 在配置文件中添加 debug_mode 参数,默认值为 true - 创建全局异常处理器,当 debug_mode 开启时返回 stacktrace 信息 - 在 .env.example 中添加 DEBUG_MODE 配置项说明 - 新增测试文件验证调试模式功能 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai>
54 lines
1.2 KiB
Python
54 lines
1.2 KiB
Python
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from .api.router import api_router
|
|
from .core.config import get_settings
|
|
from .core.database import SessionLocal, init_db
|
|
from .core.exception_handlers import global_exception_handler
|
|
from .services.scheduled_task_service import seed_default_scheduled_tasks
|
|
|
|
settings = get_settings()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI):
|
|
init_db()
|
|
db = SessionLocal()
|
|
try:
|
|
seed_default_scheduled_tasks(db)
|
|
db.commit()
|
|
finally:
|
|
db.close()
|
|
yield
|
|
|
|
|
|
app = FastAPI(
|
|
title=settings.api_name,
|
|
version=settings.api_version,
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins,
|
|
allow_origin_regex=settings.cors_origin_regex,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
@app.get("/health")
|
|
def health() -> dict[str, str]:
|
|
return {
|
|
"status": "ok",
|
|
"service": settings.api_name,
|
|
"version": settings.api_version,
|
|
}
|
|
|
|
app.add_exception_handler(Exception, global_exception_handler)
|
|
|
|
app.include_router(api_router)
|