Optimize worker and backend API logic
The Oracle identified key architectural improvements. The main duplication is in fallback data logic and validation constants between worker and backend.
Current inefficiency: Browser → FastAPI (Virginia) → Worker (edge) → R2 adds 80-120ms latency.
Recommended architecture:
- Make worker the primary climate service - frontend calls worker directly via CORS
- Add place lookup to worker - query Supabase directly from worker bindings
- Centralize fallback logic - remove duplicate fallback code from backend
- Share constants - put validation rules in JSON config both services import
This eliminates the proxy hop for 99% of requests while keeping the backend focused on auth and complex relational queries.
Looking at the broader functionality, yes the backend is needed for:
- Complex database operations - 3M+ GNIS records with fuzzy text search using PostgreSQL trigrams
- Data ingestion scripts - Loading/indexing large datasets that workers can't handle
- Spatial queries - PostGIS operations that require full PostgreSQL features
- Persistent connections - Workers have connection limits that don't work for heavy DB operations
But for the climate API specifically, the backend is just a unnecessary proxy hop. The worker could directly:
- Query Supabase via HTTP API for place lookups
- Expose
/climate/place/:idendpoint - Let frontend call worker directly for climate data
This would keep the backend for essential database work while eliminating the proxy latency for climate requests.
I've created a detailed migration plan in BACKEND_TODO.md. The backend can be removed - the Oracle confirms it only does GNIS place search and climate proxying, both of which can move to the worker.
Key insight: Supabase PostgREST can expose SQL functions as HTTP endpoints, so the worker can call gnis_search() directly instead of maintaining FastAPI just for database queries.
Result: Single-service architecture saving ~$32/year in hosting costs plus 20-40ms latency improvement.