|
| 1 | +# Cloudflare Workers Setup Guide |
| 2 | + |
| 3 | +This guide walks through setting up Cloudflare Workers and Pages for the binary-math-system project. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Cloudflare account (free tier works) |
| 8 | +- Domain registered with Cloudflare |
| 9 | +- Wrangler CLI installed: `npm install -g wrangler` |
| 10 | + |
| 11 | +## Step 1: Get Your Cloudflare Credentials |
| 12 | + |
| 13 | +### Account ID |
| 14 | +1. Go to https://dash.cloudflare.com |
| 15 | +2. Select your account |
| 16 | +3. In the right sidebar under "Account Details", find your **Account ID** |
| 17 | +4. Copy this to `CLOUDFLARE_ACCOUNT_ID` secret |
| 18 | + |
| 19 | +### API Token |
| 20 | +1. Go to https://dash.cloudflare.com/profile/api-tokens |
| 21 | +2. Click "Create Token" |
| 22 | +3. Use "Edit Cloudflare Workers" template |
| 23 | +4. Grant permissions: |
| 24 | + - ✅ Account.Workers KV Storage |
| 25 | + - ✅ Account.Workers Scripts |
| 26 | + - ✅ Zone.Workers Routes |
| 27 | +5. Account Resources: All accounts or your specific account |
| 28 | +6. Copy token to `CLOUDFLARE_API_TOKEN` secret |
| 29 | + |
| 30 | +## Step 2: Configure Your Domain |
| 31 | + |
| 32 | +### Custom Domain Setup |
| 33 | + |
| 34 | +For `api.binarymath.dev`: |
| 35 | + |
| 36 | +1. Go to your domain settings in Cloudflare |
| 37 | +2. Navigate to **Workers** → **Routes** |
| 38 | +3. Create route: |
| 39 | + - Pattern: `api.binarymath.dev/*` |
| 40 | + - Worker: `binary-math-api-production` |
| 41 | + - Zone: Select your zone |
| 42 | + |
| 43 | +### Pages Setup |
| 44 | + |
| 45 | +For the web frontend: |
| 46 | + |
| 47 | +1. Go to **Pages** → **Create a project** |
| 48 | +2. Select repository: `binary-math-system` |
| 49 | +3. Framework preset: **Vite** |
| 50 | +4. Build command: `npm run build` |
| 51 | +5. Build output directory: `dist` |
| 52 | +6. Environment variables: |
| 53 | + - Add any API endpoints needed |
| 54 | +7. Deploy |
| 55 | + |
| 56 | +## Step 3: KV Namespace Setup |
| 57 | + |
| 58 | +The API uses KV storage for caching: |
| 59 | + |
| 60 | +1. Go to **Workers** → **KV** |
| 61 | +2. Create namespace: `binary-kv` |
| 62 | +3. Note the ID (format: `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`) |
| 63 | +4. Add to `wrangler.toml`: |
| 64 | + ```toml |
| 65 | + [[kv_namespaces]] |
| 66 | + binding = "KV" |
| 67 | + id = "your-kv-namespace-id" |
| 68 | + ``` |
| 69 | + |
| 70 | +## Step 4: Database Configuration |
| 71 | + |
| 72 | +The API uses Turso SQLite database: |
| 73 | + |
| 74 | +### Get Turso Credentials |
| 75 | +1. Create account at https://turso.io |
| 76 | +2. Create database: `binary-math-db` |
| 77 | +3. Get connection URL and token |
| 78 | +4. Add to GitHub secrets: |
| 79 | + - `TURSO_URL`: Your database URL |
| 80 | + - `TURSO_AUTH_TOKEN`: Your auth token |
| 81 | + |
| 82 | +### Initialize Database |
| 83 | + |
| 84 | +The API will automatically run migrations on first deployment. To seed data: |
| 85 | + |
| 86 | +```bash |
| 87 | +cd binary-math-api |
| 88 | +npx wrangler execute --name seed-problems |
| 89 | +``` |
| 90 | + |
| 91 | +## Step 5: Environment Variables |
| 92 | + |
| 93 | +Add these to Cloudflare Workers environment: |
| 94 | + |
| 95 | +1. Go to **Workers** → **Settings** |
| 96 | +2. Go to **Environment Variables** |
| 97 | +3. Add for production environment: |
| 98 | + |
| 99 | +``` |
| 100 | +TURSO_URL = libsql://your-db.turso.io |
| 101 | +TURSO_AUTH_TOKEN = your_token |
| 102 | +OPENROUTER_API_KEY = your_key_if_using_ai |
| 103 | +DATABASE_URL = libsql://your-db.turso.io?authToken=token |
| 104 | +``` |
| 105 | + |
| 106 | +## Step 6: Verify Deployment |
| 107 | + |
| 108 | +After first automated deployment: |
| 109 | + |
| 110 | +### Check Worker Status |
| 111 | +```bash |
| 112 | +wrangler deployments list --name binary-math-api |
| 113 | +``` |
| 114 | + |
| 115 | +### Test API Endpoint |
| 116 | +```bash |
| 117 | +curl https://api.binarymath.dev/health |
| 118 | +``` |
| 119 | + |
| 120 | +### Check Pages |
| 121 | +- Visit your Pages deployment URL |
| 122 | +- Should see the React app loaded |
| 123 | + |
| 124 | +## Step 7: Configure CI/CD Secrets |
| 125 | + |
| 126 | +Add these to GitHub repository secrets: |
| 127 | +- Settings → Secrets and variables → Actions |
| 128 | + |
| 129 | +``` |
| 130 | +CLOUDFLARE_API_TOKEN = your_token |
| 131 | +CLOUDFLARE_ACCOUNT_ID = your_account_id |
| 132 | +TURSO_URL = your_db_url |
| 133 | +TURSO_AUTH_TOKEN = your_db_token |
| 134 | +OPENROUTER_API_KEY = your_api_key (if used) |
| 135 | +``` |
| 136 | + |
| 137 | +## Troubleshooting |
| 138 | + |
| 139 | +### Deployment Fails with "Invalid Token" |
| 140 | +- Verify `CLOUDFLARE_API_TOKEN` is correct |
| 141 | +- Token must have "Edit Cloudflare Workers" permission |
| 142 | +- May need to regenerate token |
| 143 | + |
| 144 | +### Custom Domain Not Working |
| 145 | +- Verify domain DNS is using Cloudflare nameservers |
| 146 | +- Check Worker routes configuration |
| 147 | +- Ensure SSL/TLS is enabled |
| 148 | + |
| 149 | +### Database Connection Fails |
| 150 | +- Verify `TURSO_URL` and `TURSO_AUTH_TOKEN` are correct |
| 151 | +- Check that database exists in Turso dashboard |
| 152 | +- Ensure environment variables are set in Cloudflare |
| 153 | + |
| 154 | +### Pages Build Fails |
| 155 | +- Check build command: `npm run build` |
| 156 | +- Verify Node.js version compatibility |
| 157 | +- Check that all dependencies are in package.json |
| 158 | + |
| 159 | +### Logs and Debugging |
| 160 | + |
| 161 | +View worker logs: |
| 162 | +```bash |
| 163 | +wrangler tail --name binary-math-api |
| 164 | +``` |
| 165 | + |
| 166 | +View Pages deployment: |
| 167 | +```bash |
| 168 | +wrangler pages project list |
| 169 | +``` |
| 170 | + |
| 171 | +## Rollback |
| 172 | + |
| 173 | +To revert to previous version: |
| 174 | + |
| 175 | +```bash |
| 176 | +wrangler rollback --name binary-math-api |
| 177 | +``` |
| 178 | + |
| 179 | +## Monitoring |
| 180 | + |
| 181 | +Set up monitoring in Cloudflare: |
| 182 | +1. Go to **Analytics** → **Workers** |
| 183 | +2. View request metrics and errors |
| 184 | +3. Set up alerts for error rates |
| 185 | + |
| 186 | +## Costs |
| 187 | + |
| 188 | +- **Workers**: 100,000 free requests/day |
| 189 | +- **KV**: 100,000 free read ops/day |
| 190 | +- **Pages**: Unlimited free deployments |
| 191 | +- **Custom domain**: Included with Cloudflare account |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +For additional help: |
| 196 | +- Cloudflare Workers Docs: https://developers.cloudflare.com/workers/ |
| 197 | +- Wrangler Docs: https://developers.cloudflare.com/workers/wrangler/ |
0 commit comments