A lightweight and scalable API Gateway in Go designed for microservice architectures. It offloads critical cross-cutting concerns by providing centralized JWT authentication, distributed tier based rate limiting with Redis, and resilience through circuit breakers, all configured dynamically via Consul.
- Dynamic Routing: Routes are loaded from Consul in real-time. Add, remove, or modify routes with no gateway restarts (hot-reloading).
- Centralized Middleware:
- JWT Authentication: Secure routes with JWT validation.
- Tiered Rate Limiting: Apply flexible, per-route, per-user-tier rate limits using the Sliding Window Log algorithm with Redis.
- Circuit Breaker: Automatically detects failing downstream services and opens the circuit to prevent cascading failures.
- Scalable Architecture: Built on a stateless model for easy horizontal scaling.
- Observability: Structured JSON logging for easy parsing and analysis.
- Containerized: The entire environment (gateway, services, Consul, Redis) is managed via Docker Compose for easy, one-command setup.
- Go (version 1.23+)
- Docker and Docker Compose
-
Clone the repository:
git clone https://github.com/sahasourav17/goGateway.git cd goGateway -
Build and run the entire environment: This single command starts the gateway, two mock services, Consul, and Redis.
docker-compose up --build
(You can add the
-dflag to run it in the background) -
Load the initial configuration into Consul: In a new terminal window, run this command to upload the routing rules. The gateway will detect this and configure itself automatically.
docker exec -i consul consul kv put gateway/config - < ./config/config.json
-
Access the Consul UI: You can view the configuration and service health in your browser at
http://localhost:8500.
To access protected routes, you need a valid JWT.
- Go to jwt.io.
- Set the algorithm to HS256.
- Set the secret key to
fast-secure-scalable-api-gw-built-from-scratchor you can change it to whatever you want - Use one of the following payloads:
- Default User:
{"user_id": "user-default-123"} - Premium User:
{"user_id": "user-premium-456", "tier": "premium"}
- Default User:
- Copy the generated token to use in the examples below.
-
Test a Public Route (Rate Limit: 5 req/min)
curl -i http://localhost:8080/public/users/health
-
Test a Protected Route (No Token - Should Fail)
curl -i http://localhost:8080/api/users/profile # Expected: HTTP/1.1 401 Unauthorized -
Test a Protected Route (With Token) Replace
YOUR_JWTwith a token you generated.curl -i -H "Authorization: Bearer YOUR_JWT" http://localhost:8080/api/users/profile # Expected: HTTP/1.1 200 OK # Check the response headers for RateLimit-* details!
