67 lines
1.4 KiB
Python
67 lines
1.4 KiB
Python
"""
|
||
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,
|
||
)
|