""" FastAPI应用入口点 基本面选股系统后端服务 """ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from contextlib import asynccontextmanager from app.core.config import settings from app.core.database import engine, Base from app.routers import reports, config, progress @asynccontextmanager async def lifespan(app: FastAPI): """应用生命周期管理""" # 启动时创建数据库表 async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) yield # 关闭时清理资源 await engine.dispose() # 创建FastAPI应用实例 app = FastAPI( title="基本面选股系统", description=""" 提供股票基本面分析和报告生成的API服务 ## 功能特性 * **报告管理**: 创建、查询、更新和删除股票分析报告 * **进度追踪**: 实时追踪报告生成进度 * **配置管理**: 管理系统配置,包括数据库、API密钥等 * **多市场支持**: 支持中国、香港、美国、日本股票市场 ## 支持的市场 * `china` - 中国A股市场 * `hongkong` - 香港股票市场 * `usa` - 美国股票市场 * `japan` - 日本股票市场 """, version="1.0.0", lifespan=lifespan, docs_url="/docs", redoc_url="/redoc", openapi_url="/openapi.json" ) # 配置CORS中间件 app.add_middleware( CORSMiddleware, allow_origins=settings.ALLOWED_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 注册路由 app.include_router( reports.router, prefix="/api/reports", tags=["reports"], responses={ 404: {"description": "报告不存在"}, 500: {"description": "服务器内部错误"} } ) app.include_router( config.router, prefix="/api/config", tags=["config"], responses={ 400: {"description": "配置参数错误"}, 500: {"description": "服务器内部错误"} } ) app.include_router( progress.router, prefix="/api/progress", tags=["progress"], responses={ 404: {"description": "进度记录不存在"}, 500: {"description": "服务器内部错误"} } ) @app.get("/") async def root(): """根路径健康检查""" return {"message": "基本面选股系统API服务正在运行", "version": "1.0.0"} @app.get("/health") async def health_check(): """健康检查端点""" return {"status": "healthy", "service": "fundamental-stock-analysis"}