# Server Recommendation System - Setup Guide ## 1. Create Cloudflare Resources ### Create D1 Database ```bash # Create D1 database npx wrangler d1 create server-recommend-db # Copy the database_id from output and update wrangler.toml ``` ### Create KV Namespace ```bash # Create KV namespace for caching npx wrangler kv:namespace create CACHE # Copy the id from output and update wrangler.toml ``` ## 2. Initialize Database Schema ```bash # Execute schema.sql against your D1 database npx wrangler d1 execute server-recommend-db --file=schema.sql ``` ## 3. Seed Sample Data (Optional) Create a file `seed.sql` with sample server data and run: ```bash npx wrangler d1 execute server-recommend-db --file=seed.sql ``` ## 4. Development ```bash # Start local development server npm run dev # Type checking npm run typecheck ``` ## 5. Deployment ```bash # Deploy to Cloudflare Workers npm run deploy ``` ## API Endpoints ### Health Check ```bash curl https://your-worker.workers.dev/api/health ``` ### Get Servers ```bash curl "https://your-worker.workers.dev/api/servers?minCpu=4&minMemory=8" ``` ### Request Recommendations ```bash curl -X POST https://your-worker.workers.dev/api/recommend \ -H "Content-Type: application/json" \ -d '{ "cpu_cores": 4, "memory_gb": 8, "storage_gb": 100, "network_bandwidth_mbps": 1000, "sla_requirement": 99.9, "budget_monthly": 200, "regions": ["us-east-1", "us-west-2"] }' ``` ## Environment Variables No environment variables needed - all bindings are configured in `wrangler.toml`: - `AI`: Workers AI binding (automatic) - `DB`: D1 Database binding - `CACHE`: KV namespace binding ## Testing Workers AI Integration The worker uses `@cf/meta/llama-3.1-8b-instruct` model. Test with: ```bash # Local development automatically uses Workers AI npm run dev # Make test request curl -X POST http://localhost:8787/api/recommend \ -H "Content-Type: application/json" \ -d '{ "cpu_cores": 2, "memory_gb": 4, "storage_gb": 50 }' ``` ## Cache Strategy Recommendations are cached in KV for 1 hour based on request parameters: - Cache key format: `recommend:cpu:X|mem:Y|stor:Z|...` - TTL: 3600 seconds (1 hour) - Cache hit/miss logged in console ## Error Handling All endpoints return proper HTTP status codes: - `200`: Success - `400`: Invalid request (validation error) - `404`: Endpoint not found - `500`: Internal server error CORS is enabled for all origins (`Access-Control-Allow-Origin: *`). ## Monitoring Enable observability in `wrangler.toml`: ```toml [observability] enabled = true ``` View logs: ```bash npx wrangler tail ```