Files
PolyWeather/web/app_factory.py
T
2569718930@qq.com 23882ac1ef SSE Patch 增量推送架构全链路实现
- web/sse_manager.py: asyncio.Queue 连接池 + broadcast + event_stream
- web/routers/sse_router.py: GET /api/events + POST /api/internal/collector-patch
- weather_sources.py: 采集后 POST patch 给 web
- Nginx polyweather.conf: /api/events proxy_buffering off
- frontend/api/events/route.ts: Next.js SSE 代理
- frontend/hooks/use-sse-patches.ts: EventSource + useLatestPatch + 重连
- LiveTemperatureThresholdChart: mergePatchIntoHourly 增量合并
- scan-terminal-query: 接入 SSE patch 更新行数据
- 新增 ssePatchArchitecture.test.ts 架构测试
2026-05-26 10:39:17 +08:00

49 lines
1.8 KiB
Python

"""Application assembly for the PolyWeather FastAPI backend.
This module centralizes router registration while preserving the existing
``web.core.app`` singleton and middleware setup during the transition toward a
more modular backend structure.
"""
import os
from fastapi import FastAPI
from web.core import app as core_app
from web.routers.analytics import router as analytics_router
from web.routers.city import router as city_router
from web.routers.auth import router as auth_router
from web.routers.ops import router as ops_router
from web.routers.payments import router as payments_router
from web.routers.scan import router as scan_router
from web.routers.sse_router import router as sse_router
from web.routers.system import router as system_router
from web.routes import router as legacy_router
from web.scan_terminal_service import start_scan_terminal_prewarm
_ROUTES_REGISTERED_FLAG = "_polyweather_routes_registered"
def _scan_terminal_prewarm_enabled() -> bool:
return str(
os.getenv("POLYWEATHER_SCAN_TERMINAL_PREWARM_ENABLED") or "false"
).strip().lower() in {"1", "true", "yes", "on"}
def create_app() -> FastAPI:
"""Return the configured FastAPI app with routers registered once."""
if not bool(getattr(core_app.state, _ROUTES_REGISTERED_FLAG, False)):
core_app.include_router(system_router)
core_app.include_router(city_router)
core_app.include_router(auth_router)
core_app.include_router(analytics_router)
core_app.include_router(scan_router)
core_app.include_router(sse_router)
core_app.include_router(payments_router)
core_app.include_router(ops_router)
core_app.include_router(legacy_router)
setattr(core_app.state, _ROUTES_REGISTERED_FLAG, True)
if _scan_terminal_prewarm_enabled():
start_scan_terminal_prewarm()
return core_app