xxy aa98ea2623 @
Initial commit

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@
2026-06-05 18:45:29 +08:00

67 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
main.py
报告生成独立服务 FastAPI 入口。
启动方式:
uvicorn main:app --reload
python main.py
"""
import logging
from contextlib import asynccontextmanager
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from config import settings
from database import engine, init_database
from log import configure_logging
from routers import report
configure_logging()
_log = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""应用启动与关闭时执行。"""
init_database()
yield
engine.dispose()
app = FastAPI(
lifespan=lifespan,
title=settings.APP_TITLE,
version=settings.APP_VERSION,
description=settings.APP_DESCRIPTION,
docs_url="/docs",
redoc_url="/redoc",
)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(report.router, prefix="/api/v1")
@app.get("/health", tags=["系统"], summary="健康检查")
def health_check():
"""确认服务存活,返回版本信息。"""
return {"status": "ok", "version": settings.APP_VERSION}
if __name__ == "__main__":
uvicorn.run(
"main:app",
host=settings.HOST,
port=settings.PORT,
reload=settings.RELOAD,
)