# Cloudflare Workers for Rocky Mountain Vending This directory contains Cloudflare Workers for handling API endpoints and serving content from R2 buckets. ## Account Information - **Account ID**: `bd6f76304a840ba11b75f9ced84264f4` - **Temp Subdomain**: `matt-bd6.workers.dev` ## Workers ### API Worker (`api-worker`) Main worker that handles all API endpoints: - `/api/manuals/*` - Serve PDF manuals from R2 bucket - `/api/thumbnails/*` - Serve thumbnail images from R2 bucket - `/api/sitemap-submit` - Submit sitemap to Google Search Console - `/api/request-indexing` - Request URL indexing in Google Search Console - `/health` - Health check endpoint ## Prerequisites 1. **R2 Buckets**: Create the following buckets in Cloudflare Dashboard: - `vending-vm-manuals` - For PDF manuals - `vending-vm-thumbnails` - For thumbnail images 2. **Wrangler CLI**: Install Wrangler if not already installed: ```bash npm install -g wrangler ``` 3. **Authentication**: Login to Cloudflare: ```bash wrangler login ``` ## Setup ### 1. Create R2 Buckets ```bash # Create manuals bucket wrangler r2 bucket create vending-vm-manuals # Create thumbnails bucket wrangler r2 bucket create vending-vm-thumbnails ``` ### 2. Configure R2 Bucket Access In Cloudflare Dashboard: 1. Go to R2 → Select bucket → Settings 2. Enable public access if you want public URLs 3. Configure CORS if accessing from different domains ### 3. Set Environment Variables (Optional) For Google Search Console integration: ```bash # Set Google service account email wrangler secret put GOOGLE_SERVICE_ACCOUNT_EMAIL # Set Google private key (paste the full key including \n) wrangler secret put GOOGLE_PRIVATE_KEY # Set site URL wrangler secret put GOOGLE_SITE_URL # Enter: https://rockymountainvending.com # Set main site URL wrangler secret put SITE_URL # Enter: https://rockymountainvending.com ``` ### 4. Deploy the Worker From the `code` directory: ```bash # Deploy to production wrangler deploy # 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` ## Usage ### Serve Manuals ``` GET https://rocky-mountain-vending-api.matt-bd6.workers.dev/api/manuals/Crane/manual.pdf ``` ### Serve Thumbnails ``` GET https://rocky-mountain-vending-api.matt-bd6.workers.dev/api/thumbnails/Crane/manual.jpg ``` ### Submit Sitemap ```bash curl -X POST https://rocky-mountain-vending-api.matt-bd6.workers.dev/api/sitemap-submit \ -H "Content-Type: application/json" \ -d '{"sitemapUrl": "https://rockymountainvending.com/sitemap.xml"}' ``` ### Request Indexing ```bash curl -X POST https://rocky-mountain-vending-api.matt-bd6.workers.dev/api/request-indexing \ -H "Content-Type: application/json" \ -d '{"url": "https://rockymountainvending.com/manuals"}' ``` ### Health Check ``` GET https://rocky-mountain-vending-api.matt-bd6.workers.dev/health ``` ## Custom Domain Setup To use a custom domain instead of the workers.dev subdomain: 1. Go to Cloudflare Dashboard → Workers & Pages 2. Select your worker 3. Go to Settings → Triggers 4. Add a custom domain or route Example routes: - `api.rockymountainvending.com/*` - Route all API requests - `rockymountainvending.com/api/*` - Route API requests from main domain ## R2 Bucket Structure ### Manuals Bucket (`vending-vm-manuals`) Store PDF files with manufacturer folders: ``` Crane/ manual1.pdf manual2.pdf Royal Vendors/ manual1.pdf ... ``` ### Thumbnails Bucket (`vending-vm-thumbnails`) Store thumbnail images matching manual structure: ``` Crane/ manual1.jpg manual2.jpg Royal Vendors/ manual1.jpg ... ``` ## Development ### Local Development ```bash # Start local development server wrangler dev # Start with specific environment wrangler dev --env development ``` ### Testing ```bash # Test health endpoint curl https://rocky-mountain-vending-api.matt-bd6.workers.dev/health # Test manual endpoint (requires file in R2) curl https://rocky-mountain-vending-api.matt-bd6.workers.dev/api/manuals/Crane/manual.pdf ``` ## Troubleshooting ### Worker Not Found - Verify account ID in `wrangler.toml` matches your Cloudflare account - Check that you're logged in: `wrangler whoami` ### R2 Bucket Not Found - Verify bucket names match in `wrangler.toml` - Check buckets exist: `wrangler r2 bucket list` - Ensure buckets are in the same account ### CORS Errors - Add CORS headers in worker response - Configure CORS in R2 bucket settings if using public URLs - Check allowed origins in worker code ### Google Search Console API The Google Search Console integration requires additional setup: 1. Enable Google Search Console API in Google Cloud Console 2. Create service account and download credentials 3. Grant service account access in Google Search Console 4. Install `googleapis` library (may require bundling for Workers) See `SEO_SETUP.md` for detailed instructions. ## Monitoring View worker logs and metrics in Cloudflare Dashboard: - Workers & Pages → Your Worker → Logs - Workers & Pages → Your Worker → Metrics ## Cost Considerations - **Workers**: Free tier includes 100,000 requests/day - **R2**: Pay-as-you-go storage and egress - **Custom Domains**: Included with Workers Monitor usage in Cloudflare Dashboard → Billing.