|
| 1 | +# Maintenance Guide — ShackDesk Backend |
| 2 | + |
| 3 | +## Cloudflare API Token |
| 4 | + |
| 5 | +The `CF_API_TOKEN` GitHub Actions secret authorises Wrangler to deploy Workers. |
| 6 | + |
| 7 | +**To rotate:** |
| 8 | +1. Cloudflare Dashboard → **My Profile → API Tokens → Create Token** |
| 9 | +2. Use the **Edit Cloudflare Workers** template |
| 10 | +3. Add D1 write permission if not included |
| 11 | +4. Copy the token |
| 12 | +5. GitHub → `Computer-Consultant/ShackDesk-Backend` → **Settings → Secrets → Actions** |
| 13 | +6. Update `CF_API_TOKEN` |
| 14 | +7. Delete the old token from the Cloudflare dashboard |
| 15 | + |
| 16 | +**CF_ACCOUNT_ID** is not a secret but is stored as a GitHub secret for convenience. Find it on the Cloudflare dashboard right sidebar of any zone page. |
| 17 | + |
| 18 | +## D1 Database |
| 19 | + |
| 20 | +**Database name:** `shackdesk-telemetry` |
| 21 | +**Database ID:** `e77c8a3d-cba7-4326-846c-faeb2c585da0` |
| 22 | + |
| 23 | +### Query the database |
| 24 | +```bash |
| 25 | +wrangler d1 execute shackdesk-telemetry --remote --command="SELECT * FROM reports ORDER BY received_at DESC LIMIT 20;" |
| 26 | +``` |
| 27 | + |
| 28 | +### Data retention — manual purge (90-day policy) |
| 29 | +```bash |
| 30 | +wrangler d1 execute shackdesk-telemetry --remote \ |
| 31 | + --command="DELETE FROM reports WHERE received_at < datetime('now', '-90 days');" |
| 32 | +``` |
| 33 | + |
| 34 | +This should be run periodically. Automating it via a Cloudflare Cron Trigger is a future improvement. |
| 35 | + |
| 36 | +### Schema migrations |
| 37 | + |
| 38 | +Schema changes require a migration file. Never alter the live schema directly without testing locally first. |
| 39 | + |
| 40 | +1. Write the migration SQL |
| 41 | +2. Test locally: `wrangler d1 execute shackdesk-telemetry --local --file=migration.sql` |
| 42 | +3. Apply to production: `wrangler d1 execute shackdesk-telemetry --remote --file=migration.sql` |
| 43 | +4. Commit the migration file to the repo under `db/migrations/` |
| 44 | + |
| 45 | +## Adding a New Worker |
| 46 | + |
| 47 | +1. Create `workers/<name>/src/index.js` |
| 48 | +2. Add a new `[[routes]]` block and any bindings to `wrangler.toml` |
| 49 | +3. If the new Worker needs its own D1 database, create it: `wrangler d1 create <db-name>` |
| 50 | +4. Add a deploy step to `.github/workflows/deploy.yml` if it needs a separate pipeline |
| 51 | + |
| 52 | +## Local Development |
| 53 | + |
| 54 | +```bash |
| 55 | +wrangler dev |
| 56 | +``` |
| 57 | + |
| 58 | +Wrangler creates a local SQLite database at `.wrangler/state/`. This is gitignored. |
| 59 | + |
| 60 | +To seed the local database with the schema: |
| 61 | +```bash |
| 62 | +wrangler d1 execute shackdesk-telemetry --local --file=db/schema.sql |
| 63 | +``` |
| 64 | + |
| 65 | +## DNS |
| 66 | + |
| 67 | +`telemetry.shackdesk.com` — CNAME record in the Cloudflare zone, proxied (orange cloud). |
| 68 | +Wrangler registers the route automatically on deploy via `wrangler.toml`. If the route ever disappears, redeploy: `wrangler deploy`. |
| 69 | + |
| 70 | +## Cloudflare Rate Limiting |
| 71 | + |
| 72 | +Rate limiting is not in code — it is configured in the Cloudflare dashboard. |
| 73 | + |
| 74 | +**Location:** Cloudflare Dashboard → shackdesk.com zone → **Security → WAF → Rate Limiting Rules** |
| 75 | + |
| 76 | +Recommended rule for `/report`: |
| 77 | +- Expression: `http.request.uri.path eq "/report"` |
| 78 | +- Threshold: 30 requests per 60 seconds |
| 79 | +- Action: Block (duration: 60 seconds) |
0 commit comments