nomadro/apps/api/services/ebook_svc/routers/track.py
Eric 017008dd7c Harden weapp/API P0 paths: VIP auth, ebook login, WAL limits, game stub.
Drop Three from weapp game, mount VIP library on unified API, and auto-enable local debug without manual .env.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-11 19:06:50 +08:00

33 lines
1.1 KiB
Python

from __future__ import annotations
from typing import Annotated
from fastapi import APIRouter, Header, Request
from ebook_svc.core.rate_limit import rate_limit
from ebook_svc.schemas.track import TrackBatchBody
from ebook_svc.services.store import store
from ebook_svc.services.track import ingest_events
router = APIRouter(prefix="/track", tags=["track"])
@router.post("")
@router.post("/")
async def track_batch(
body: TrackBatchBody,
request: Request,
authorization: Annotated[str | None, Header()] = None,
):
"""小程序埋点上报。可匿名;若带 Bearer 则绑定 openid。"""
rate_limit(request, bucket="track", limit=120, window_sec=60.0)
openid = (body.openid or "").strip() or None
if authorization and authorization.startswith("Bearer "):
token = authorization.removeprefix("Bearer ").strip()
user = store.get_user_by_token(token)
if user:
openid = user.get("openid") or openid
events = [e.model_dump() for e in body.events]
return await ingest_events(events, openid=openid, client=body.client or "miniapp")