Monorepo for Aesthetic.Computer aesthetic.computer
at main 267 lines 5.9 kB view raw view rendered
1# Grab Worker - Browser Rendering Service 2 3Screenshot and preview generation service using Cloudflare Browser Rendering API. 4 5## Files 6 7- `.env` - Environment variables for grab worker deployment 8- `wrangler.production.toml` - Cloudflare Workers production configuration 9 10## Usage 11 12### Loading Secrets in DevContainer 13 14Secrets are automatically loaded via `/aesthetic-computer-vault/devault.fish` on container start. 15 16### Manual Secret Loading 17 18```fish 19# Load environment variables (fish shell) 20set -gx (cat /workspaces/aesthetic-computer/aesthetic-computer-vault/grab/.env | grep -v '^#' | string split '=') 21``` 22 23### Deploying Grab Worker 24 25```fish 26# Copy secrets to working directory 27cd /workspaces/aesthetic-computer/aesthetic-computer-vault 28./devault.fish 29 30# Deploy 31cd /workspaces/aesthetic-computer/grab 32npm run deploy:production 33``` 34 35### Setting Cloudflare Secrets 36 37```fish 38cd /workspaces/aesthetic-computer/grab 39 40# If you have sensitive API keys (not currently needed) 41wrangler secret put BROWSER_API_KEY 42``` 43 44## Initial Setup 45 46### 1. Get Cloudflare API Token (for DNS automation) 47 48**Required for automatic DNS configuration during deployment.** 49 501. Go to: https://dash.cloudflare.com/profile/api-tokens 512. Click: **"Create Token"** 523. Use template: **"Edit zone DNS"** 534. Configure: 54 - **Permissions:** Zone → DNS → Edit 55 - **Zone Resources:** Include → Specific zone → aesthetic.computer 565. Click: **"Continue to summary"** → **"Create Token"** 576. Copy the token and add to `.env`: 58 ```bash 59 CLOUDFLARE_API_TOKEN=your-token-here 60 ``` 61 62**Note:** This is different from `CLOUDFLARE_API_KEY` (Global API Key). The API Token has scoped permissions for DNS only. 63 64### 2. Create Browser Rendering Binding 65 66```fish 67cd /workspaces/aesthetic-computer/grab 68./scripts/setup-browser-binding.fish 69``` 70 71This will: 72- Enable Browser Rendering in your Cloudflare account 73- Create the binding 74- Output the binding ID 75- Update the `.env` file with the ID 76 77### 3. Create Durable Object Namespace 78 79```fish 80wrangler dispatch-namespace create SCREENSHOT_DO 81``` 82 83Copy the namespace ID to `.env`: 84```bash 85DO_NAMESPACE_ID=<paste-id-here> 86``` 87 88### 4. Create R2 Bucket (Optional) 89 90```fish 91wrangler r2 bucket create aesthetic-screenshots 92``` 93 94Copy the bucket name/ID to `.env`. 95 96### 5. Deploy with Automatic DNS 97 98**One-command deployment with automatic DNS configuration:** 99 100```fish 101cd /workspaces/aesthetic-computer/grab 102./scripts/deploy-with-dns.fish 103``` 104 105This script will: 1061. ✅ Deploy the worker to Cloudflare 1072. ✅ Automatically create/update DNS CNAME record 1083. ✅ Wait for DNS propagation 1094. ✅ Verify the deployment is accessible 110 111**Manual deployment (without DNS automation):** 112 113```fish 114cd /workspaces/aesthetic-computer/grab 115npm run deploy:production 116 117# Then manually configure DNS via Dashboard or use: 118./scripts/configure-dns.fish 119``` 120 121### 6. Alternative: Manual DNS Configuration 122 123If the automated script doesn't work, you can manually configure DNS: 124 125**Option A: Cloudflare Dashboard (2 minutes)** 1261. Go to: https://dash.cloudflare.com/a23b54e8877a833a1cf8db7765bce3ca/aesthetic.computer/dns/records 1272. Click: "Add record" 1283. Type: CNAME 1294. Name: `grab` 1305. Target: `aesthetic-grab.aesthetic-computer.workers.dev` 1316. Proxy: ✅ Enabled (orange cloud) 1327. Save 133 134**Option B: Workers Dashboard (5 minutes)** 1351. Go to Workers & Pages 1362. Click on `aesthetic-grab` 1373. Go to Settings > Domains & Routes 1384. Click "Add Custom Domain" 1395. Enter: `grab.aesthetic.computer` 1406. Click "Add Domain" 141 142Either method works - automated deployment script is fastest! 143 144## Architecture 145 146``` 147Client Request (grab.aesthetic.computer) 148149Cloudflare Worker 150151Check Cache API (ETag) 152 ↓ (miss) 153Durable Object (per URL) 154155Browser Rendering API 156157Puppeteer Screenshot 158159Store in Cache + R2 160161Return PNG 162``` 163 164## Endpoints 165 166### Icon Generation 167``` 168GET https://grab.aesthetic.computer/icon/128x128/prompt~wipe.png 169GET https://grab.aesthetic.computer/icon/128x128/welcome.png 170``` 171 172### Preview Generation (OG Image) 173``` 174GET https://grab.aesthetic.computer/preview/1200x630/prompt~wipe.png 175``` 176 177### Preview Generation (Twitter Card) 178``` 179GET https://grab.aesthetic.computer/preview/1800x900/prompt~wipe.png 180``` 181 182## Monitoring 183 184### View Logs 185```fish 186# Real-time logs 187wrangler tail aesthetic-grab 188 189# Production logs 190wrangler tail aesthetic-grab --env production 191``` 192 193### Metrics 194 195View in Cloudflare Dashboard: 196- Workers & Pages > aesthetic-grab > Metrics 197- Analytics > Workers 198- Durable Objects > SCREENSHOT_DO 199 200## Troubleshooting 201 202### Browser Rendering Not Working 203 2041. Check browser binding is enabled: 205```fish 206wrangler whoami 207# Ensure Browser Rendering is in enabled features 208``` 209 2102. Check DO namespace exists: 211```fish 212wrangler dispatch-namespace list 213``` 214 2153. Check logs: 216```fish 217wrangler tail aesthetic-grab 218``` 219 220### Caching Issues 221 222Clear CDN cache: 223```fish 224# Via Cloudflare Dashboard: 225# Caching > Configuration > Purge Cache 226# Or use Cloudflare API 227``` 228 229### Performance Issues 230 231- Check browser timeout settings 232- Monitor DO session count 233- Check R2 storage limits 234- Review cache hit rates 235 236## Security 237 238- All secrets stored in vault (not in repo) 239- API keys managed via wrangler secrets 240- Rate limiting per IP 241- URL validation (aesthetic.computer only) 242- Resource limits enforced 243 244## Cost Optimization 245 246### Browser Rendering 247- Charged per second of browser time 248- Optimize page load times 249- Cache aggressively 250- Set reasonable timeouts 251 252### Durable Objects 253- Charged per request and duration 254- Minimize DO calls via caching 255- Batch operations when possible 256 257### R2 Storage 258- Class A operations (write) 259- Class B operations (read) 260- Storage capacity 261- Monitor usage via dashboard 262 263## Related Documentation 264 265- Main implementation: `/workspaces/aesthetic-computer/grab/` 266- Deployment plan: `/workspaces/aesthetic-computer/grab/PLAN.md` 267- Migration guide: `/workspaces/aesthetic-computer/grab/DEPLOYMENT.md`