# Cloudflare Workers Deployment Guide This guide walks you through deploying the Cloudflare Workers for Rocky Mountain Vending. ## Quick Start ```bash # 1. Install Wrangler (if not already installed) npm install -g wrangler # 2. Login to Cloudflare wrangler login # 3. Create R2 buckets wrangler r2 bucket create vending-vm-manuals wrangler r2 bucket create vending-vm-thumbnails # 4. Deploy the worker cd code wrangler deploy ``` ## Step-by-Step Instructions ### 1. Prerequisites - Cloudflare account with Account ID: `bd6f76304a840ba11b75f9ced84264f4` - Node.js and npm installed - Wrangler CLI installed ### 2. Install Wrangler ```bash npm install -g wrangler ``` ### 3. Authenticate with Cloudflare ```bash wrangler login ``` This will open a browser window to authenticate with Cloudflare. ### 4. Verify Account ```bash wrangler whoami ``` You should see your account information including the Account ID. ### 5. Create R2 Buckets Create the buckets for storing manuals and thumbnails: ```bash # Create manuals bucket wrangler r2 bucket create vending-vm-manuals # Create thumbnails bucket wrangler r2 bucket create vending-vm-thumbnails ``` Verify buckets were created: ```bash wrangler r2 bucket list ``` ### 6. Configure R2 Bucket Access (Optional) If you want public access to R2 buckets: 1. Go to Cloudflare Dashboard → R2 2. Select each bucket → Settings 3. Enable "Public Access" if needed 4. Configure CORS if accessing from different domains ### 7. Upload Files to R2 (Optional) If you have manuals and thumbnails to upload: ```bash # Upload a manual wrangler r2 object put vending-vm-manuals/Crane/manual.pdf --file=path/to/manual.pdf # Upload a thumbnail wrangler r2 object put vending-vm-thumbnails/Crane/manual.jpg --file=path/to/thumbnail.jpg ``` Or use the existing upload script: ```bash cd code node scripts/upload-to-r2.js --type all ``` ### 8. Set Environment Variables (Optional) For Google Search Console integration: ```bash # Set Google service account email wrangler secret put GOOGLE_SERVICE_ACCOUNT_EMAIL # Paste: your-service-account@project.iam.gserviceaccount.com # Set Google private key wrangler secret put GOOGLE_PRIVATE_KEY # Paste the full private key (including \n characters) # Set site URL for Google Search Console wrangler secret put GOOGLE_SITE_URL # Enter: https://rockymountainvending.com # Set main site URL wrangler secret put SITE_URL # Enter: https://rockymountainvending.com ``` ### 9. Deploy the Worker From the `code` directory: ```bash # Deploy to production wrangler deploy # Or deploy to development environment wrangler deploy --env development ``` The worker will be available at: - Production: `https://rocky-mountain-vending-api.matt-bd6.workers.dev` - Development: `https://rocky-mountain-vending-api-dev.matt-bd6.workers.dev` ### 10. Test the Deployment ```bash # Test health endpoint curl https://rocky-mountain-vending-api.matt-bd6.workers.dev/health # Test manual endpoint (if you uploaded a file) curl https://rocky-mountain-vending-api.matt-bd6.workers.dev/api/manuals/Crane/manual.pdf ``` ## Custom Domain Setup To use a custom domain instead of `*.workers.dev`: 1. Go to Cloudflare Dashboard → Workers & Pages 2. Select your worker: `rocky-mountain-vending-api` 3. Go to Settings → Triggers 4. Add a custom domain or route ### Option 1: Custom Domain Add a custom domain like `api.rockymountainvending.com`: - The domain must be in your Cloudflare account - SSL will be automatically provisioned ### Option 2: Route Add a route from your main domain: - Route: `rockymountainvending.com/api/*` - This routes all `/api/*` requests to the worker ## Updating the Worker To update the worker after making changes: ```bash cd code wrangler deploy ``` ## Viewing Logs View real-time logs: ```bash wrangler tail ``` Or view in Cloudflare Dashboard: - Workers & Pages → Your Worker → Logs ## Monitoring View metrics in Cloudflare Dashboard: - Workers & Pages → Your Worker → Metrics You can see: - Request count - Error rate - CPU time - Subrequest count ## Troubleshooting ### Error: Account ID mismatch Verify the account ID in `wrangler.toml` matches your Cloudflare account: ```bash wrangler whoami ``` ### Error: Bucket not found Verify buckets exist: ```bash wrangler r2 bucket list ``` If buckets don't exist, create them: ```bash wrangler r2 bucket create vending-vm-manuals wrangler r2 bucket create vending-vm-thumbnails ``` ### Error: Worker deployment failed Check for syntax errors: ```bash cd code wrangler dev ``` ### CORS Errors Ensure CORS headers are set in the worker response. The worker already includes CORS headers, but you may need to configure CORS in R2 bucket settings if using public URLs. ### Files Not Found - Verify files exist in R2 buckets - Check file paths match the request URLs - Ensure bucket bindings are correct in `wrangler.toml` ## Rollback To rollback to a previous version: 1. Go to Cloudflare Dashboard → Workers & Pages 2. Select your worker 3. Go to Deployments 4. Find the previous version and click "Promote to production" ## Cost Considerations - **Workers Free Tier**: 100,000 requests/day - **R2 Storage**: $0.015/GB/month - **R2 Egress**: $0.09/GB (first 10GB free per month) - **Custom Domains**: Included with Workers Monitor usage in Cloudflare Dashboard → Billing. ## Next Steps 1. **Upload Content**: Upload manuals and thumbnails to R2 buckets 2. **Configure Custom Domain**: Set up custom domain for API endpoints 3. **Set Up Monitoring**: Configure alerts for errors and high usage 4. **Google Search Console**: Complete Google Search Console API setup for SEO features ## Support For issues or questions: - Check Cloudflare Workers documentation: https://developers.cloudflare.com/workers/ - Check R2 documentation: https://developers.cloudflare.com/r2/ - View worker logs: `wrangler tail` or Cloudflare Dashboard