44 lines
1.0 KiB
Python
44 lines
1.0 KiB
Python
"""FastAPI 入口。
|
||
|
||
启动时自动建表(MVP 用 create_all;阶段1 切 Alembic 迁移,含 TimescaleDB hypertable)。
|
||
"""
|
||
from contextlib import asynccontextmanager
|
||
|
||
from fastapi import FastAPI
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
|
||
from .api import router
|
||
from .db import Base, engine
|
||
from . import models # noqa: F401 —— 注册 ORM 到 Base.metadata
|
||
|
||
|
||
@asynccontextmanager
|
||
async def lifespan(app: FastAPI):
|
||
async with engine.begin() as conn:
|
||
await conn.run_sync(Base.metadata.create_all)
|
||
yield
|
||
|
||
|
||
app = FastAPI(
|
||
title="Stock Backtest",
|
||
description="历史回测 + 回放式模拟平台(A 股为主,不做实盘)",
|
||
version="0.1.0",
|
||
lifespan=lifespan,
|
||
)
|
||
|
||
# 开发期允许前端 dev server 跨域;上线收窄 origins
|
||
app.add_middleware(
|
||
CORSMiddleware,
|
||
allow_origins=["*"],
|
||
allow_credentials=True,
|
||
allow_methods=["*"],
|
||
allow_headers=["*"],
|
||
)
|
||
|
||
app.include_router(router)
|
||
|
||
|
||
@app.get("/")
|
||
async def root() -> dict:
|
||
return {"name": "Stock Backtest API", "docs": "/docs"}
|