feat(cloud): the online ordering app (takeaway, delivery) with hours, time slots and rate limits (platform-v2 3S, S.4)
- connect_frontend/order-app at /order/{site}: menu per service at that service's prices,
extras, basket (kept per tab), checkout priced by the cloud (fee, area by list or
postcode, minimum), name/phone/address/notes (remembered), as soon as possible or a time
slot, tracking page with steps, lines and totals; en/el; tokens in index.css. Logic in
src/logic.js, unit-tested; built and served by the connect image (npm test in the build).
- services/ordering.py: the venue's services, hours (past midnight), preparation, slot grid,
days ahead, ASAP; takeaway/delivery orders checked (order.service_unavailable,
order.closed, order.time_invalid) and need a phone; GET /api/orders/{site}/slots; the
public menu carries the ordering summary.
- services/ratelimit.py: per-caller limits on quote, order and track (429 rate.limited).
- online_orders.requested_for (0003, unreleased) stored as UTC; every datetime sent with its
offset (the database keeps no zone: a 12:00 Athens slot came back as 12:00 unzoned).
- Tracking returns what was ordered, the totals, the type and the time.
- Tests: test_ordering.py (5), checkout/fees updated; order-app unit (4).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
"""A small in-memory rate limit for the public endpoints (CL-4, plan phase 3S S.4).
|
||||
|
||||
hit(bucket, key, limit, per_seconds) -> bool True = allowed (and counted)
|
||||
client_ip(request) the caller's address behind the proxy
|
||||
|
||||
Per process (the cloud runs one); a restart forgets. Enough to stop a script hammering the
|
||||
order endpoint; not a defence against a distributed attack (that's the proxy's job).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
import time
|
||||
from collections import deque
|
||||
|
||||
_lock = threading.Lock()
|
||||
_hits: dict[tuple[str, str], deque] = {}
|
||||
_MAX_KEYS = 50_000
|
||||
|
||||
|
||||
def hit(bucket: str, key: str, limit: int, per_seconds: float, now: float | None = None) -> bool:
|
||||
now = time.monotonic() if now is None else now
|
||||
with _lock:
|
||||
if len(_hits) > _MAX_KEYS: # never grow without bound
|
||||
_hits.clear()
|
||||
q = _hits.setdefault((bucket, key), deque())
|
||||
while q and now - q[0] >= per_seconds:
|
||||
q.popleft()
|
||||
if len(q) >= limit:
|
||||
return False
|
||||
q.append(now)
|
||||
return True
|
||||
|
||||
|
||||
def reset() -> None:
|
||||
with _lock:
|
||||
_hits.clear()
|
||||
|
||||
|
||||
def client_ip(request) -> str:
|
||||
"""The first X-Forwarded-For address (the VPS proxy sets it), else the socket's."""
|
||||
fwd = request.headers.get("x-forwarded-for", "")
|
||||
if fwd:
|
||||
return fwd.split(",")[0].strip() or "?"
|
||||
return getattr(request.client, "host", None) or "?"
|
||||
Reference in New Issue
Block a user