"""会话存档独立 poller → app-server 的内部信号端点(server→server, 非客户端接口)。 会话存档轮询在【独立进程】跑(WeWorkFinanceSdk 是 Go c-shared 库, 嵌进 app-server 会 segfault 把主进程带崩, 见 scripts/wx_finance_poller.py)。poller 拉到美团卡片/截图后 POST 到这里, 由 app-server 打比价信号 —— set_pending 在 app-server 进程内存, 心跳才 pop 得到, 故必须回到本进程。 靠共享密钥头 X-Internal-Secret(== settings.INTERNAL_API_SECRET)校验;未配置 → 503。 """ from __future__ import annotations import hmac import logging from typing import Annotated from fastapi import APIRouter, Header, HTTPException, status from app.core import wx_poc_signal from app.core.config import settings from app.schemas.wx_finance import WxFinancePendingIn, WxFinancePendingOut logger = logging.getLogger("shagua.internal.wx_finance") router = APIRouter(prefix="/internal", tags=["internal"]) def _check_secret(x_internal_secret: str | None) -> None: """共享密钥校验。未配置 → 503(挡裸奔);不匹配 → 401(常量时间比较)。""" configured = settings.INTERNAL_API_SECRET if not configured: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="internal api not configured", ) if not x_internal_secret or not hmac.compare_digest(x_internal_secret, configured): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="invalid internal secret" ) @router.post( "/wx-finance/pending", response_model=WxFinancePendingOut, summary="会话存档 poller 触发比价信号(独立进程→app-server, 打 set_pending)", ) def wx_finance_pending( payload: WxFinancePendingIn, x_internal_secret: Annotated[str | None, Header()] = None, ) -> WxFinancePendingOut: _check_secret(x_internal_secret) device_id = settings.WX_POC_TEST_DEVICE_ID if not device_id: logger.warning("wx_finance pending: WX_POC_TEST_DEVICE_ID 未配置, 忽略信号") return WxFinancePendingOut(ok=False) wx_poc_signal.set_pending(device_id, payload.source) logger.info( "wx_finance PoC: 已给测试设备 %s 打比价信号 source=%s (触发=%s)", device_id, payload.source, payload.kind, ) return WxFinancePendingOut(ok=True)