52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
"""FastAPI 入口。数据库结构统一由 Alembic 管理。"""
|
||
from contextlib import asynccontextmanager
|
||
|
||
from fastapi import FastAPI
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
from sqlalchemy import text
|
||
|
||
from .api import router
|
||
from .auth_api import router as auth_router
|
||
from .config import settings
|
||
from .db import engine
|
||
|
||
|
||
@asynccontextmanager
|
||
async def lifespan(app: FastAPI):
|
||
async with engine.connect() as conn:
|
||
await conn.execute(text("SELECT 1"))
|
||
yield
|
||
await engine.dispose()
|
||
|
||
|
||
app = FastAPI(
|
||
title="Stock Backtest",
|
||
description="股票研究平台:全市场数据 + 智能选股 + 事件回测(A 股为主,不做实盘)",
|
||
version="0.1.0",
|
||
lifespan=lifespan,
|
||
docs_url="/docs" if settings.expose_api_docs else None,
|
||
redoc_url=None,
|
||
openapi_url="/openapi.json" if settings.expose_api_docs else None,
|
||
)
|
||
|
||
app.add_middleware(
|
||
CORSMiddleware,
|
||
allow_origins=settings.allowed_origins,
|
||
allow_credentials=True,
|
||
allow_methods=["*"],
|
||
allow_headers=["*"],
|
||
)
|
||
|
||
app.include_router(auth_router)
|
||
app.include_router(router)
|
||
|
||
|
||
@app.get("/api/health")
|
||
async def health() -> dict:
|
||
return {"status": "ok"}
|
||
|
||
|
||
@app.get("/")
|
||
async def root() -> dict:
|
||
return {"name": "Stock Backtest API", "docs": "/docs"}
|