A production-ready API Gateway built with Go, featuring JWT authentication, rate limiting, load balancing, and circuit breakers for microservices architecture.
- 🔐 JWT Authentication - Secure token-based authentication with user management
- 🛡️ Rate Limiting - Token bucket algorithm preventing abuse (100 req/min per IP)
- ⚖️ Load Balancing - Round-robin distribution across service instances
- 🔌 Circuit Breaker - Automatic failure detection and recovery
- 📊 Request Logging - Structured JSON logs with request tracing
- 🏥 Health Checks - Kubernetes-ready liveness and readiness probes
- 🔒 Security Headers - HSTS, CSP, X-Frame-Options, and more
- 🌐 CORS Support - Configurable cross-origin resource sharing
- Go 1.21 or higher
- Docker & Docker Compose
- MongoDB 7.0+ (auto-started with Docker)
- Redis 7.0+ (auto-started with Docker)
cd Api-Gateway
go mod download && go mod tidy# Build and start all services (Gateway + MongoDB + Redis)
docker-compose -f deployments/docker-compose.yml build
docker-compose -f deployments/docker-compose.yml up -d
# Verify services are running
docker-compose -f deployments/docker-compose.yml psExpected: 3 containers running (gateway, mongo, redis)
curl http://localhost:8080/healthExpected Response:
{
"success": true,
"message": "Service is healthy",
"data": { "status": "healthy", "version": "1.0.0" }
}curl -X POST http://localhost:8080/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"alice","email":"alice@example.com","password":"password123"}'Response: Returns JWT token and user details
curl -X POST http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"alice","password":"password123"}'curl -X GET http://localhost:8080/api/v1/profile \
-H "Authorization: Bearer YOUR_JWT_TOKEN"Save as test-gateway.ps1 and run .\test-gateway.ps1:
# Register User
$body = @{ username = "alice"; email = "alice@example.com"; password = "password123" } | ConvertTo-Json
$response = Invoke-WebRequest -Uri http://localhost:8080/api/v1/auth/register -Method POST -Body $body -ContentType "application/json"
$token = ($response.Content | ConvertFrom-Json).data.token
# Get Profile
$headers = @{ "Authorization" = "Bearer $token" }
Invoke-WebRequest -Uri http://localhost:8080/api/v1/profile -Method GET -Headers $headers| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /health |
Health check | No |
| GET | /ready |
Readiness probe | No |
| POST | /api/v1/auth/register |
Register new user | No |
| POST | /api/v1/auth/login |
User login | No |
| POST | /api/v1/auth/refresh |
Refresh JWT token | No |
| GET | /api/v1/profile |
Get user profile | Yes |
| ANY | /api/v1/users/* |
Proxy to users service | Yes |
| ANY | /api/v1/products/* |
Proxy to products service | Yes |
| ANY | /api/v1/orders/* |
Proxy to orders service | Yes |
| GET | /api/v1/admin/services |
List services | Yes (Admin) |
📖 Full API Documentation: See docs/API.md
Api-Gateway/
├── cmd/gateway/ # Main application entry point
├── internal/
│ ├── config/ # Configuration management
│ ├── middleware/ # Auth, rate limit, logging, CORS
│ ├── handler/ # HTTP request handlers
│ ├── service/ # Service registry & load balancer
│ ├── circuit/ # Circuit breaker implementation
│ └── models/ # Data models
├── pkg/
│ ├── logger/ # Structured logging
│ ├── storage/ # MongoDB & Redis clients
│ └── utils/ # JWT & response utilities
├── config/ # Configuration files
├── deployments/ # Docker & Kubernetes configs
└── docs/ # Documentation
Configuration via environment variables or config/config.yaml:
server:
port: 8080
environment: production
jwt:
secret: your-secret-key
expiry: 24h
rate_limit:
requests: 100
window: 60sEnvironment Variables:
PORT=8080
JWT_SECRET=your-secret-key
MONGO_URI=mongodb://localhost:27017
REDIS_ADDR=localhost:6379docker-compose -f deployments/docker-compose.yml down- Algorithm: Token bucket with automatic refill
- Default: 100 requests per 60 seconds per IP
- Storage: Redis-backed for distributed rate limiting
- Headers: Returns
X-RateLimit-*headers in responses
- Threshold: 5 consecutive failures
- Timeout: 30 seconds recovery period
- States: Closed → Open → Half-Open → Closed
- Benefit: Prevents cascading failures across services
- JWT token expiry: 24 hours (configurable)
- Password hashing: bcrypt with salt
- Security headers: HSTS, CSP, X-Frame-Options
- CORS: Configurable allowed origins
- API Documentation - Complete API reference with examples
- Architecture - System design and components
- Configuration Guide - All configuration options
Made with ❤️ using Go