Build an edge function that routes requests by geography, applies rate limiting, and transforms responses — all at the edge for minimal latency.
## Task Edge function that acts as an intelligent API gateway with geolocation routing. ## Requirements - Runtime: Cloudflare Workers, Deno Deploy, or Vercel Edge - No Node.js APIs — Web Standards only (fetch, Request, Response) ## Specifications ```typescript // Edge middleware that: // 1. Detects user's country/city from cf-ipcountry or geo headers // 2. Routes to nearest backend: // US → us-east.api.example.com // EU → eu-west.api.example.com // APAC → ap-south.api.example.com // 3. Applies rate limiting (token bucket, stored in KV/Durable Object) // 4. Adds security headers (CORS, CSP, HSTS) // 5. Caches GET responses at edge (stale-while-revalidate) // 6. Transforms response: strip internal headers, add timing info ``` ## Implementation Notes 1. Use Web Crypto for rate limit token hashing 2. Rate limit: 100 req/min per IP, stored in Workers KV or Durable Objects 3. Cache control: 60s for GET, no-cache for POST/PUT/DELETE 4. Fallback: if nearest region is down, route to next closest 5. Add X-Edge-Location and X-Response-Time headers for debugging 6. Total edge processing should be <5ms
No gallery images yet.