GateLite

A lightweight API gateway built from scratch in Python. Zero external dependencies. Production patterns, educational code.

24
Modules
298
Tests
0
Dependencies
7k
Lines
โญ View on GitHub ๐Ÿ“ Read the Article

Request Pipeline

Clientโ†’ CORSโ†’ Middlewareโ†’ Authโ†’ Rate Limitโ†’ Routeโ†’ Cacheโ†’ Transformโ†’ Circuit Breakerโ†’ Load Balanceโ†’ Proxyโ†’ Upstream

Core Features

๐Ÿ”€ Smart Routing

Exact, prefix, parameterized, and regex path matching with priority-based resolution. Path parameters extracted automatically.

โš–๏ธ Load Balancing

Round-robin, weighted round-robin, least connections, IP hash, and random algorithms with per-upstream connection tracking.

๐Ÿšฆ Rate Limiting

Token bucket, fixed window, and sliding window log algorithms. Per-client limiting with configurable rates and burst capacity.

๐Ÿ” Authentication

API key, JWT (HMAC-SHA256), and HTTP Basic auth. Pluggable pipeline with public route exemptions and user context propagation.

โšก Circuit Breaker

Per-upstream circuit breakers with CLOSED โ†’ OPEN โ†’ HALF-OPEN state machine. Configurable failure thresholds and recovery timeouts.

๐Ÿ’พ Response Cache

LRU cache with TTL, Cache-Control header support, conditional requests via ETag/304, and automatic invalidation on mutations.

๐Ÿ”„ Retry + Timeout

Configurable retry policies with exponential backoff, jitter, and retry budgets. Per-route timeout deadlines with connect/read/overall limits.

๐Ÿฅ Health Checking

Active background probes and passive request-based tracking. Configurable healthy/unhealthy thresholds with status change callbacks.

๐Ÿ”Œ Plugin System

Extensible lifecycle hooks โ€” on_startup, on_request, on_response, on_error, on_shutdown. Built-in IP whitelist and maintenance mode plugins.

๐Ÿ“Š Metrics + Admin

Request counts, latency percentiles (p50/p95/p99), cache hit rates, and circuit breaker stats. JSON admin API at /admin/*.

๐ŸŒ CORS

Full preflight and simple request handling. Configurable origins, methods, credentials, exposed headers, and max-age.

๐Ÿ”ง Transformations

Header injection/removal, path rewriting (strip/add prefix), security headers, and JSON response envelope wrapping.

Quick Start

from gatelite.config import GatewayConfig
from gatelite.server import GatewayServer

config = GatewayConfig.from_dict({
    "gateway": {"port": 8080},
    "routes": [
        {"name": "api", "path": "/api/*", "upstream": "backend"},
    ],
    "upstreams": {
        "backend": {
            "targets": [{"host": "127.0.0.1", "port": 5000}],
        },
    },
})

server = GatewayServer(config)
server.start()  # Listening on :8080