Worker Deployment
Create the D1 Database
Section titled “Create the D1 Database”cd workerwrangler d1 create email-gatewayCopy the returned database_id into wrangler.toml:
[[d1_databases]]binding = "DB"database_name = "email-gateway"database_id = "your-actual-database-id"Environment Variables
Section titled “Environment Variables”Create a worker/.dev.vars file for local development:
AUTH_TOKEN=a-strong-random-tokenDESTINATION_EMAIL=your@email.comThis file is gitignored and loaded automatically by Wrangler during wrangler dev.
For production, set secrets via the CLI:
wrangler secret put AUTH_TOKENwrangler secret put DESTINATION_EMAILRun Migrations
Section titled “Run Migrations”# Local D1 (for development)bun run db:migrate:local
# Remote D1 (production)bun run db:migrate:remoteDeploy
Section titled “Deploy”bun run deployThis deploys the worker with:
- HTTP API (Hono router with bearer auth)
- Email handler (receives from Cloudflare Email Routing)
- Durable Object (WebSocket hub for real-time updates)
Seed Default Settings
Section titled “Seed Default Settings”After deploying, configure the default settings:
curl -X PUT https://email-gateway.your-account.workers.dev/api/settings \ -H "Authorization: Bearer YOUR_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "destination_email": "you@example.com", "auto_forward_allowed": "true", "auto_reject_blocked": "true" }'Local Development
Section titled “Local Development”bun run devThis starts a local wrangler dev server with D1 and Durable Objects emulated locally.