Skip to content

A high-performance, cloud-native API Gateway built in Go. It uses Consul for dynamic, zero-downtime route configuration and provides centralized middleware for authentication, rate limiting, and circuit breaking.

License

Notifications You must be signed in to change notification settings

sahasourav17/goGateway

Repository files navigation

goGateway

Go Version License

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.


Core Features

  • 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.

Architecture Diagram

goGateway Architecture

Getting Started

Prerequisites

  • Go (version 1.23+)
  • Docker and Docker Compose

Running the Project

  1. Clone the repository:

    git clone https://github.com/sahasourav17/goGateway.git
    cd goGateway
  2. 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 -d flag to run it in the background)

  3. 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
  4. Access the Consul UI: You can view the configuration and service health in your browser at http://localhost:8500.

API Testing & Endpoints

Generating a Test JWT

To access protected routes, you need a valid JWT.

  1. Go to jwt.io.
  2. Set the algorithm to HS256.
  3. Set the secret key to fast-secure-scalable-api-gw-built-from-scratch or you can change it to whatever you want
  4. Use one of the following payloads:
    • Default User: {"user_id": "user-default-123"}
    • Premium User: {"user_id": "user-premium-456", "tier": "premium"}
  5. Copy the generated token to use in the examples below.

Example curl Commands

  • 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_JWT with 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!

About

A high-performance, cloud-native API Gateway built in Go. It uses Consul for dynamic, zero-downtime route configuration and provides centralized middleware for authentication, rate limiting, and circuit breaking.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published