Files
auv/backend/main.py
T
Sakurasan d7d019c2c4 feat: 市场看板 - 聚合同花顺SDK全量数据的A股实时看板
- 新增 /dashboard 页面:暗色主题,指数行情/市场温度/涨跌分布/行业强度/概念热度/事件情报
- 后端聚合接口 /api/market-dashboard,30秒缓存
- 利用SDK接口:指数行情、全市场快照、涨停/跌停/炸板池、连板天梯、热门股、飙升榜、龙虎榜、异动分析、集合竞价基准、行业/概念目录
- 市场温度评分:6因子加权(涨跌比/中位涨跌/强弱比/涨停活跃度/炸板惩罚/竞价信号)
- 首页添加市场看板导航入口
2026-08-31 10:29:37 +08:00

72 lines
2.7 KiB
Python

import asyncio
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from contextlib import asynccontextmanager
from dotenv import load_dotenv
from database import init_db
from routes import stock, collections, shares, themes, core_stocks, fuyao, market_dashboard
from services.daily_collector import collector_loop, cache_cleanup_loop
load_dotenv()
@asynccontextmanager
async def lifespan(app: FastAPI):
init_db()
collector_task = asyncio.create_task(collector_loop())
cache_cleanup_task = asyncio.create_task(cache_cleanup_loop())
try:
yield
finally:
for t in (collector_task, cache_cleanup_task):
t.cancel()
for t in (collector_task, cache_cleanup_task):
try:
await t
except asyncio.CancelledError:
pass
app = FastAPI(title="AUV API", version="1.0.0", lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(stock.router, prefix="/api/stock")
app.include_router(collections.router, prefix="/api/collections")
app.include_router(shares.router, prefix="/api/share")
app.include_router(themes.router, prefix="/api/themes")
app.include_router(core_stocks.router, prefix="/api/core-stocks")
app.include_router(fuyao.router, prefix="/api/v2")
app.include_router(market_dashboard.router, prefix="/api")
# 生产模式:后端同时托管前端静态文件
# catch-all 路由在 API 路由之后注册,所以 API 优先级更高
dist_path = os.path.join(os.path.dirname(__file__), "dist")
# HTML 不缓存:保证 index.html 永远最新(引用的资源文件名带 hash,可长缓存)
_NO_CACHE_HTML = {"Cache-Control": "no-cache, no-store, must-revalidate"}
if os.path.isdir(dist_path):
@app.get("/{full_path:path}")
async def serve_spa(full_path: str):
file_path = os.path.join(dist_path, full_path) if full_path else os.path.join(dist_path, "index.html")
if os.path.isfile(file_path):
# 静态资源(带 hash 的文件名)可缓存;HTML 不缓存
if file_path.endswith(".html"):
return FileResponse(file_path, headers=_NO_CACHE_HTML)
return FileResponse(file_path)
# SPA fallback: 非文件路径统一返回 index.html
index_path = os.path.join(dist_path, "index.html")
if os.path.isfile(index_path):
return FileResponse(index_path, media_type="text/html", headers=_NO_CACHE_HTML)
return JSONResponse({"detail": "Not Found"}, status_code=404)