Spaces:
Running

File size: 11,698 Bytes
25834e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
services:
  app:
    image: ghcr.io/spoo-me/spoo:${IMAGE_TAG:-latest}
    container_name: spoo_app
    restart: always
    command: >
      uv run uvicorn app.main:app
      --host 0.0.0.0
      --port 8000
      --workers 2
      --no-access-log
      --timeout-keep-alive 5
      --timeout-graceful-shutdown 30
    env_file:
      - .env.production
    depends_on:
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://localhost:8000/health"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 30s
    mem_limit: 3g
    mem_reservation: 512m
    networks:
      spoonet:
        ipv4_address: 172.30.0.10
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
        tag: "{{.Name}}"

  qr:
    image: ghcr.io/spoo-me/qr:${QR_IMAGE_TAG:-latest}
    container_name: spoo_qr
    restart: always
    command: >
      uv run uvicorn main:app
      --host 0.0.0.0
      --port 8080
      --workers 2
      --no-access-log
      --timeout-keep-alive 5
      --timeout-graceful-shutdown 30
    env_file:
      - .env.qr
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://localhost:8080/health"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 20s
    mem_limit: 512m
    mem_reservation: 128m
    networks:
      spoonet:
        ipv4_address: 172.30.0.60
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
        tag: "{{.Name}}"

  redis:
    image: redis:7-alpine
    container_name: spoo_redis
    restart: always
    # 512MB cap (out of 8GB box). LRU evicts stale URL entries when full.
    # No persistence — cache is rebuildable from Mongo. RDB/AOF disabled to
    # avoid disk I/O cost. Lose-cache-on-restart is acceptable.
    command: >
      redis-server
      --maxmemory 512mb
      --maxmemory-policy allkeys-lru
      --save ""
      --appendonly no
      --tcp-keepalive 60
      --timeout 300
      --requirepass ${REDIS_PASSWORD}
    environment:
      # redis-cli reads REDISCLI_AUTH automatically — healthcheck can
      # authenticate without the password appearing on a process cmdline.
      REDISCLI_AUTH: ${REDIS_PASSWORD}
    volumes:
      - redis-data:/data
    healthcheck:
      # grep is load-bearing: redis-cli exits 0 even on NOAUTH/error replies
      test: ["CMD-SHELL", "redis-cli ping | grep -q PONG"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 5s
    mem_limit: 768m
    networks:
      spoonet:
        ipv4_address: 172.30.0.40
    logging:
      driver: json-file
      options:
        max-size: "5m"
        max-file: "2"

  caddy:
    image: caddy:2-alpine
    container_name: spoo_caddy
    restart: always
    # Caddy terminates TLS for CF orange-cloud requests using a Cloudflare
    # Origin CA certificate (mounted from ./caddy/origin.{crt,key}). UFW on
    # the host (see deploy/ufw-cloudflare.sh) restricts inbound 443
    # to CF IP ranges only — so any request reaching Caddy came from CF.
    #
    # Architecturally this replaces cloudflared. CF Tunnel routed all traffic
    # through IAD POPs which has flaky cross-region backbone for some user
    # paths (Jio IPv6 → MRS, Berlin POP for Asia traffic). Direct origin uses
    # CF's standard anycast — no tunnel detour.
    ports:
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./caddy/Caddyfile:/etc/caddy/Caddyfile:ro
      - ./caddy/origin.crt:/etc/caddy/origin.crt:ro
      - ./caddy/origin.key:/etc/caddy/origin.key:ro
      - ./caddy/cf-aop-zone-ca.crt:/etc/caddy/cf-aop-zone-ca.crt:ro
      - caddy-data:/data
      - caddy-config:/config
    depends_on:
      app:
        condition: service_healthy
      qr:
        condition: service_healthy
    # Caddy admin API check is the cheapest reliable liveness signal.
    # An https://localhost/health probe fails on SNI (localhost matches no
    # host block); an external probe through Cloudflare loops back through
    # the host network. Admin API on 127.0.0.1:2019 proves the daemon is
    # accepting config — that's the failure mode we actually want to catch.
    healthcheck:
      test: ["CMD", "wget", "-qO-", "--tries=1", "--timeout=3", "http://127.0.0.1:2019/config/"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    # raised from 256m after the 2026-06-20 OOM incident (slow upstream →
    # buffered responses); keep in sync with the value live on the VPS
    mem_limit: 1g
    mem_reservation: 64m
    networks:
      spoonet:
        ipv4_address: 172.30.0.20
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
        tag: "{{.Name}}"

  redis-exporter:
    image: oliver006/redis_exporter:latest
    container_name: spoo_redis_exporter
    restart: always
    environment:
      - REDIS_ADDR=redis://redis:6379
      - REDIS_PASSWORD=${REDIS_PASSWORD}
    depends_on:
      redis:
        condition: service_healthy
    # No healthcheck: the redis_exporter image is built FROM scratch (Go binary
    # only — no shell, wget, curl, or nc). Docker compose has no built-in HTTP
    # probe (that's a k8s feature). Process auto-restarts on crash via
    # restart: always; in 14h+ uptime we've never seen this image deadlock.
    mem_limit: 64m
    networks:
      spoonet:
        ipv4_address: 172.30.0.50
    logging:
      driver: json-file
      options:
        max-size: "2m"
        max-file: "2"

  vector:
    image: timberio/vector:0.43.0-alpine
    container_name: spoo_vector
    restart: always
    command: ["--config-toml", "/etc/vector/vector.toml"]
    environment:
      - AXIOM_TOKEN=${AXIOM_TOKEN}
      - AXIOM_DATASET=${AXIOM_DATASET:-spoo-prod}
      - AXIOM_METRICS_DATASET=${AXIOM_METRICS_DATASET:-spoo-metrics}
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - ./vector.toml:/etc/vector/vector.toml:ro
    depends_on:
      - redis-exporter
    healthcheck:
      # Vector's API server (enabled in vector.toml [api]) exposes /health on
      # 127.0.0.1:8686. Catches deadlocks where process is up but pipeline is
      # stuck — restart: always alone can't see this.
      test: ["CMD", "wget", "-qO-", "--tries=1", "--timeout=3", "http://127.0.0.1:8686/health"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    mem_limit: 128m
    networks:
      spoonet:
        ipv4_address: 172.30.0.30
    logging:
      driver: json-file
      options:
        max-size: "5m"
        max-file: "2"

  # ── Click event pipeline ───────────────────────────────────────────────
  # Consumed by the app when CLICK_EVENTS_SINK=stream is set in
  # .env.production (with CLICK_EVENTS_QUEUE_REDIS_URI pointing here).
  # Removing that env var reverts the app to inline tracking; these two
  # services can then be stopped independently.
  redis-queue:
    image: redis:8-alpine
    container_name: spoo_redis_queue
    restart: always
    # Queue semantics ≠ cache semantics (spoo_redis): noeviction rejects
    # writes when full instead of silently dropping events — the app's
    # sink then falls back to inline tracking (alertable via the
    # click_sink_fallback log event). AOF everysec bounds hard-crash loss
    # to ~1s of in-flight events; the stream is the source of truth for
    # clicks between XADD and consumer XACK, unlike the rebuildable URL
    # cache which stays persistence-free.
    command: >
      redis-server
      --maxmemory 128mb
      --maxmemory-policy noeviction
      --appendonly yes
      --appendfsync everysec
      --tcp-keepalive 60
      --timeout 300
      --requirepass ${REDIS_QUEUE_PASSWORD}
    environment:
      REDISCLI_AUTH: ${REDIS_QUEUE_PASSWORD}
    volumes:
      - redis-queue-data:/data
    healthcheck:
      # grep is load-bearing: redis-cli exits 0 even on NOAUTH/error replies
      test: ["CMD-SHELL", "redis-cli ping | grep -q PONG"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 5s
    mem_limit: 192m
    networks:
      spoonet:
        ipv4_address: 172.30.0.45
    logging:
      driver: json-file
      options:
        max-size: "5m"
        max-file: "2"

  click-worker:
    image: ghcr.io/spoo-me/spoo:${IMAGE_TAG:-latest}
    container_name: spoo_click_worker
    restart: always
    command: >
      uv run uvicorn --factory app.workers.click_worker:create_app
      --host 0.0.0.0 --port 8001
      --no-access-log
      --timeout-graceful-shutdown 30
    env_file:
      - .env.production
    environment:
      # Pinned here (not .env.production) so the worker can run in stream
      # mode while the app is still on inline — the staged-rollout split.
      # ${REDIS_QUEUE_PASSWORD} interpolates from /opt/spoo/.env.
      CLICK_EVENTS_SINK: stream
      CLICK_EVENTS_QUEUE_REDIS_URI: redis://:${REDIS_QUEUE_PASSWORD}@172.30.0.45:6379/0
      # log-only hot-URL detection (50 clicks/60s per URL); feeds the
      # future CF-KV promotion + scam-analysis consumers
      CLICK_EVENTS_HOTNESS_ENABLED: "true"
    depends_on:
      redis-queue:
        condition: service_healthy
    healthcheck:
      # /health pings the queue Redis through the FastStream broker —
      # catches a wedged consumer loop, which restart: always cannot see.
      test: ["CMD", "curl", "-fsS", "http://localhost:8001/health"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 20s
    mem_limit: 512m
    mem_reservation: 128m
    networks:
      spoonet:
        ipv4_address: 172.30.0.70
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
        tag: "{{.Name}}"

  # Next frontend. Caddy routes page paths here; everything else (short
  # codes, auth, api) still hits app directly. Health check is wget, not
  # curl: the runtime image is bare node:22-alpine, and deploy.yml reads
  # .State.Health.Status to decide rollback.
  next:
    image: ghcr.io/spoo-me/frontend:${NEXT_IMAGE_TAG:-edge}
    container_name: spoo_next
    restart: always
    environment:
      - SPOO_API_URL=http://spoo.me:8000
    # SSR dials spoo.me so its Host header is the one the tenant resolver
    # expects. Scoped to this container: a network alias would also make the
    # app resolve its own domain internally, and the link tools' outbound
    # fetches need the public address. Pinned to app's ipv4_address above.
    extra_hosts:
      - "spoo.me:172.30.0.10"
    depends_on:
      app:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-qO-", "--tries=1", "--timeout=3", "http://127.0.0.1:3000/api/health"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 20s
    mem_limit: 512m
    mem_reservation: 128m
    networks:
      spoonet:
        ipv4_address: 172.30.0.80
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
        tag: "{{.Name}}"

volumes:
  redis-data:
  caddy-data:
  caddy-config:
  redis-queue-data:

networks:
  spoonet:
    driver: bridge
    ipam:
      config:
        - subnet: 172.30.0.0/24