The API Gateway is the single entry point for all client requests. It provides:
- Centralized Authentication: Firebase token verification
- Request Routing: Proxies requests to Composite Service
- Header Injection: Adds
x-firebase-uidheader for downstream services - CORS Handling: Manages cross-origin requests
Frontend β API Gateway (8000) β Composite Service (8004) β Atomic Services
- Port: 8000
- Authentication: Firebase ID token verification
- Deployment: Cloud Run
- Python 3.9+
- Firebase service account key
- Firebase project with Authentication enabled
-
Install dependencies
pip install -r requirements.txt
-
Add Firebase service account key
- Download from Firebase Console
- Place as
serviceAccountKey.jsonin service directory
-
Configure environment variables Create a
.envfile:COMPOSITE_SERVICE_URL=http://localhost:8004 FIREBASE_SERVICE_ACCOUNT_PATH=./serviceAccountKey.json GOOGLE_CLOUD_PROJECT=your-project-id
-
Run the service
uvicorn main:app --host 0.0.0.0 --port 8000
| Variable | Description | Default | Required |
|---|---|---|---|
COMPOSITE_SERVICE_URL |
Composite Service URL | http://localhost:8004 |
Yes |
FIREBASE_SERVICE_ACCOUNT_PATH |
Path to Firebase service account JSON | ./serviceAccountKey.json |
No |
GOOGLE_CLOUD_PROJECT |
GCP project ID (for ADC) | - | No |
Service information and routing details
Response:
{
"status": "API Gateway running",
"service": "api-gateway",
"version": "1.0.0",
"routes_to": {
"composite": "http://localhost:8004",
"users": "http://localhost:8001",
"events": "http://localhost:8002",
"feed": "http://localhost:8003"
}
}Health check endpoint
Response:
{
"status": "healthy",
"service": "api-gateway"
}Proxies all requests to Composite Service
Supported Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS
Headers Required:
Authorization: Bearer <firebase-token>(for authenticated endpoints)
Headers Injected:
x-firebase-uid: Firebase user ID (extracted from token)x-user-role: User role from Firebase custom claims
Handled Paths:
/api/users/*- User management/api/events/*- Event management/api/posts/*- Feed posts/api/friends/*- Friend requests- All other Composite Service endpoints
- Client Request: Client sends request with
Authorization: Bearer <firebase-token> - Token Validation: Middleware verifies token with Firebase Admin SDK
- UID Extraction: Extracts
firebase_uidandrolefrom decoded token - Header Injection: Adds
x-firebase-uidandx-user-roleheaders - Request Forwarding: Forwards to Composite Service with injected headers
- Response: Returns response from Composite Service with CORS headers
These paths are accessible without authentication:
/- Root endpoint/docs- Swagger UI/openapi.json- OpenAPI specification/redoc- ReDoc documentation/health- Health check
- Firebase Authentication Middleware: Validates all incoming tokens
- Automatic Header Injection: Adds
x-firebase-uidto all requests forwarded to Composite Service - CORS Support: Handles cross-origin requests with proper headers
- Error Handling: Returns appropriate HTTP status codes for auth failures
- Catch-All Routing: Single endpoint handles all API routes
docker build -t api-gateway .docker run -p 8000:8000 \
-e COMPOSITE_SERVICE_URL=http://composite-service:8004 \
-e GOOGLE_CLOUD_PROJECT=your-project-id \
api-gatewayThe service is deployed to Cloud Run with:
- Application Default Credentials (ADC) for Firebase
- Environment variables configured via deployment script
- No VPC Connector needed (only forwards to Cloud Run services)
curl http://localhost:8000/health# Get Firebase token from frontend after login
curl -H "Authorization: Bearer <firebase-token>" \
http://localhost:8000/api/users/mecurl -X OPTIONS \
-H "Origin: http://localhost:3000" \
-H "Access-Control-Request-Method: GET" \
http://localhost:8000/api/eventsInteractive API documentation available at:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc - OpenAPI JSON:
http://localhost:8000/openapi.json
The service returns standard HTTP status codes:
200 OK: Successful request401 Unauthorized: Missing or invalid Firebase token502 Bad Gateway: Composite Service unavailable500 Internal Server Error: Server error
- Missing Authorization Header:
401 Unauthorizedwith{"detail": "Authorization header missing"} - Invalid Token Format:
401 Unauthorizedwith{"detail": "Invalid authorization header format"} - Expired Token:
401 Unauthorizedwith{"detail": "Firebase token expired"} - Invalid Token:
401 Unauthorizedwith{"detail": "Invalid Firebase token"}
- The gateway uses a catch-all route pattern (
/{path:path}) to forward all requests - Firebase Admin SDK is initialized on service startup
- Middleware runs before route handlers to validate authentication
- CORS headers are added to all responses
- The service acts as a reverse proxy to Composite Service
When modifying the gateway:
- Update middleware in
middleware/auth_middleware.py - Add new public paths if needed
- Update this README with changes
- Test authentication flow thoroughly