Architecture
The Gateway API acts as a reverse proxy with JWT-based authentication, routing requests to specialized workers:Key Features
JWT Authentication
Secure token-based authentication with SHA-256 credential hashing
Route Proxying
Intelligent routing to BUILD, KV, and CORE services
Error Handling
Unified error response format across all routes
Service Bindings
Secure inter-worker communication via Cloudflare service bindings
Routing Matrix
The gateway routes requests based on URL prefixes:| Prefix | Route Type | Target Service | Authentication |
|---|---|---|---|
/auth/login | Authentication | Internal | None (public) |
/auth/guest | Guest Token | Internal | None (public) |
/build | Build Service | BUILD_SERVICE_URL | Required (JWT) |
/kv | KV Service | KV_SERVICE_URL | Optional (if Bearer token provided) |
/core | Core Service | CORE_SERVICE_URL | Optional (if Bearer token provided) |
Quick Start
Installation
Development
Environment Setup
The gateway requires the following environment variables:GATEWAY_JWT_SECRET- Secret key for JWT signing and verificationBUILD_SERVICE_URL- URL of the build service workerKV_SERVICE_URL- URL of the KV service workerCORE_SERVICE_URL- URL of the core service worker
R2 Bucket
The gateway uses an R2 bucket (metacogna-accounts) to store admin credentials:
- Path format:
auth/admins/{username}.json - Structure:
{ username, salt, hash, role }
Project Structure
How It Works
- Client Request: Client sends request to
api.metacogna.ai/{path} - Authentication: For protected routes, client includes
Authorization: Bearer {token}header - Route Matching: Gateway matches URL prefix to route configuration
- Token Verification: For protected routes, gateway verifies JWT token
- URL Rewriting: Gateway strips route prefix and forwards to target service
- Response Proxying: Gateway returns response from downstream service
Related Documentation
- Authentication API - Login and token generation
- Routing API - Route configuration and proxying
- Gateway Worker - Worker implementation details
- Shared Package - JWT utilities and schemas