Skip to content

Latest commit

 

History

History
100 lines (70 loc) · 2.14 KB

File metadata and controls

100 lines (70 loc) · 2.14 KB

GoGate

GoGate is a lightweight API gateway built with Go standard library HTTP primitives. It routes requests by path prefix, forwards traffic with httputil.ReverseProxy, applies middleware, and exposes small admin endpoints for route and backend health inspection.

Features

  • YAML-driven route configuration
  • Longest-prefix route matching
  • HTTP reverse proxy forwarding
  • Per-route JWT authentication
  • In-memory IP rate limiting
  • Per-route timeout control
  • Periodic backend /health checks
  • JSON access logs with method, path, client IP, status, duration, and route
  • Admin APIs for routes and health
  • Example backend service for local and Docker testing
  • Docker and Makefile workflows

Configuration

The gateway loads configs/gateway.yaml by default. Use -config <path> to load another file.

server:
  port: 8080

auth:
  enabled: true
  jwt_secret: "gateway-secret"

rate_limit:
  enabled: true
  requests_per_minute: 60

routes:
  - name: user-service
    path_prefix: /api/users
    target: http://localhost:9001
    timeout_ms: 3000
    auth_required: true

When multiple routes match a request, the longest path_prefix wins. Backends are checked at <target>/health; unhealthy routes return 503.

Local Run

Start three example backends in separate terminals:

make backend-user
make backend-order
make backend-public

Start the gateway:

make run

The gateway listens on http://localhost:8080.

Docker Run

docker compose up --build

This starts the gateway plus the three example backend services.

Example Requests

Public route:

curl http://localhost:8080/public/ping

Admin APIs:

curl http://localhost:8080/admin/routes
curl http://localhost:8080/admin/health

Authenticated routes require an HS256 JWT signed with gateway-secret:

curl -H "Authorization: Bearer <token>" http://localhost:8080/api/users/profile

Unauthenticated requests to /api/users or /api/orders return 401.

Tests

make test

This runs configuration parsing, route matching, JWT authentication, rate limiting, reverse proxy, and health monitor tests.