You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Central entry point for all API requests in the CS02 E-Commerce Platform
π Overview
This is a lightweight API Gateway implemented in Go using chi router and httputil.ReverseProxy. It serves as the single entry point for all frontend requests, handling JWT authentication, request routing, CORS, and header injection.
cd backend/api-gateway
# Build image
docker build -t cs02/apigateway:latest .# Run container
docker run -p 8080:8080 \
-e JWT_SECRET=mysecret \
-e USER_IDENTITY_SERVICE_URL=http://user-identity-service:8081 \
cs02/apigateway:latest
Using Makefile
make build # Build the binary
make run # Run the gateway
make test# Run tests
make docker # Build Docker image
π Authentication Flow
Public Routes: Requests to /api/auth/*, /api/products/*, /api/content/*, /api/ai/* pass through without auth
Protected Routes: All other routes require a valid JWT token
Token Extraction: JWT is extracted from Authorization: Bearer <token> header
Validation: Token signature verified using HS256 algorithm
Header Injection: On successful auth, gateway injects:
X-User-Subject: User's subject claim
X-User-Id: User's ID claim
X-User-Roles: User's roles claim
β Features - Completion Status
Feature
Status
Notes
Reverse proxy routing
β Complete
All services routed
JWT authentication middleware
β Complete
HS256 tokens
User info header injection
β Complete
X-User-Id, X-User-Subject, X-User-Roles
CORS handling
β Complete
Configurable origins
Health check endpoint
β Complete
/healthz
Environment variable config
β Complete
Override via env vars
YAML configuration
β Complete
config.yaml
Graceful shutdown
β Complete
Handles SIGTERM
Request logging
β Complete
Chi middleware
Overall Completion: 100% β
β Not Implemented / Future Enhancements
Feature
Priority
Notes
Rate limiting
Medium
Recommended for production
Request caching
Low
Could cache product requests
Circuit breaker
Medium
For service resilience
API versioning
Low
Currently v1 only
RS256 JWT support
Low
Currently HS256 only
Metrics/Prometheus
Medium
For monitoring
Distributed tracing
Low
OpenTelemetry integration
π Project Structure
api-gateway/
βββ main.go # Application entry point
βββ main_test.go # Unit tests
βββ config.yaml # Service configuration
βββ go.mod # Go module definition
βββ Dockerfile # Container configuration
βββ Makefile # Build commands
βββ run.sh # Development script
βββ README.md # This file
π§ͺ Testing
# Run unit tests
go test ./...
# Run with verbose output
go test -v ./...
# Test health endpoint
curl http://localhost:8080/healthz
# Test with JWT token
curl -H "Authorization: Bearer <token>" http://localhost:8080/api/users/me
π Related Services
All backend microservices are routed through this gateway: