Workers Architecture
The backend is split into four independent Cloudflare Workers that share the same D1 database and KV namespace.
Worker Matrix
| Worker | Directory | Port (dev) | Routes |
|---|---|---|---|
auth-worker | auth-worker/ | 8788 | /api/auth/*, /api/users/* |
billing-worker | billing-worker/ | 8789 | /api/billing/* |
listings-worker | listings-worker/ | 8790 | /api/listings* |
chat-worker | chat-worker/ | 8787 | /api/chat/*, /api/ws, /api/search* |
Shared Infrastructure
All workers read session tokens from the same KV namespace for auth validation. No inter-worker HTTP calls are needed — each worker queries D1 directly.
Session Flow
- User logs in via
POST /api/auth/login auth-workercreates a session token, stores it in KV assession:{token}- Browser receives
session={token}; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=604800 - On subsequent requests, any worker reads
session:{token}from KV to validate the user
CORS
All workers use the shared CORS helper from shared/src/cors.ts:
Access-Control-Allow-Credentials: true- Dynamic origin validation
- Automatic preflight handling
Auth Validation Pattern
Each worker follows the same pattern for protected endpoints:
- Read
Cookie: session={token}from the request - Look up
session:{token}in KV - If found and not expired, extract
userId - Query D1 for the full user record
- Attach user to the request context for handlers
Deployment
Each worker is deployed independently:
cd auth-worker && npm run deploycd billing-worker && npm run deploycd listings-worker && npm run deploycd chat-worker && npm run deployThis means you can ship a billing fix without redeploying chat or auth.