|
|
|
|
"""
|
|
|
|
|
FastAPI主入口
|
|
|
|
|
"""
|
|
|
|
|
from fastapi import FastAPI, Request
|
|
|
|
|
from fastapi.responses import JSONResponse, FileResponse
|
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
from app.config import settings
|
|
|
|
|
from app.api.v1 import api_router
|
|
|
|
|
from app.core.middleware import setup_middleware
|
|
|
|
|
from app.core.exceptions import (
|
|
|
|
|
business_exception_handler,
|
|
|
|
|
validation_exception_handler,
|
|
|
|
|
sqlalchemy_exception_handler,
|
|
|
|
|
general_exception_handler,
|
|
|
|
|
BusinessException
|
|
|
|
|
)
|
|
|
|
|
from app.db.session import init_db
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
|
"""应用生命周期管理"""
|
|
|
|
|
# 启动时执行
|
|
|
|
|
print(f"Starting {settings.APP_NAME}...")
|
|
|
|
|
|
|
|
|
|
# 初始化数据库
|
|
|
|
|
try:
|
|
|
|
|
init_db()
|
|
|
|
|
print("Database initialized successfully")
|
|
|
|
|
|
|
|
|
|
# 加载系统配置
|
|
|
|
|
from app.db.session import get_db
|
|
|
|
|
from app.config import load_system_configs
|
|
|
|
|
db = next(get_db())
|
|
|
|
|
load_system_configs(db)
|
|
|
|
|
print("System configs loaded successfully")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Database initialization warning: {e}")
|
|
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
# 关闭时执行
|
|
|
|
|
print(f"Shutting down {settings.APP_NAME}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 创建FastAPI应用
|
|
|
|
|
app = FastAPI(
|
|
|
|
|
title=settings.APP_NAME,
|
|
|
|
|
version=settings.APP_VERSION,
|
|
|
|
|
description="AmazingData金融数据服务平台API",
|
|
|
|
|
docs_url="/docs",
|
|
|
|
|
redoc_url="/redoc",
|
|
|
|
|
lifespan=lifespan
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 设置中间件
|
|
|
|
|
setup_middleware(app)
|
|
|
|
|
|
|
|
|
|
# 注册异常处理器
|
|
|
|
|
app.add_exception_handler(BusinessException, business_exception_handler)
|
|
|
|
|
|
|
|
|
|
# 注册路由
|
|
|
|
|
app.include_router(api_router)
|
|
|
|
|
|
|
|
|
|
# 挂载静态文件
|
|
|
|
|
static_dir = os.path.join(os.path.dirname(__file__), "static")
|
|
|
|
|
if os.path.exists(static_dir):
|
|
|
|
|
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/", tags=["根路径"])
|
|
|
|
|
async def root():
|
|
|
|
|
"""根路径 - 返回前端页面"""
|
|
|
|
|
index_file = os.path.join(static_dir, "index.html")
|
|
|
|
|
if os.path.exists(index_file):
|
|
|
|
|
return FileResponse(index_file)
|
|
|
|
|
return {
|
|
|
|
|
"name": settings.APP_NAME,
|
|
|
|
|
"version": settings.APP_VERSION,
|
|
|
|
|
"docs": "/docs",
|
|
|
|
|
"api": "/api/v1"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/health", tags=["健康检查"])
|
|
|
|
|
async def health_check():
|
|
|
|
|
"""健康检查"""
|
|
|
|
|
return {
|
|
|
|
|
"status": "healthy",
|
|
|
|
|
"version": settings.APP_VERSION
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
import uvicorn
|
|
|
|
|
uvicorn.run(
|
|
|
|
|
"app.main:app",
|
|
|
|
|
host="0.0.0.0",
|
|
|
|
|
port=8000,
|
|
|
|
|
reload=settings.DEBUG
|
|
|
|
|
)
|