A lightweight API gateway built from scratch in Python. Zero external dependencies. Production patterns, educational code.
Exact, prefix, parameterized, and regex path matching with priority-based resolution. Path parameters extracted automatically.
Round-robin, weighted round-robin, least connections, IP hash, and random algorithms with per-upstream connection tracking.
Token bucket, fixed window, and sliding window log algorithms. Per-client limiting with configurable rates and burst capacity.
API key, JWT (HMAC-SHA256), and HTTP Basic auth. Pluggable pipeline with public route exemptions and user context propagation.
Per-upstream circuit breakers with CLOSED โ OPEN โ HALF-OPEN state machine. Configurable failure thresholds and recovery timeouts.
LRU cache with TTL, Cache-Control header support, conditional requests via ETag/304, and automatic invalidation on mutations.
Configurable retry policies with exponential backoff, jitter, and retry budgets. Per-route timeout deadlines with connect/read/overall limits.
Active background probes and passive request-based tracking. Configurable healthy/unhealthy thresholds with status change callbacks.
Extensible lifecycle hooks โ on_startup, on_request, on_response, on_error, on_shutdown. Built-in IP whitelist and maintenance mode plugins.
Request counts, latency percentiles (p50/p95/p99), cache hit rates, and circuit breaker stats. JSON admin API at /admin/*.
Full preflight and simple request handling. Configurable origins, methods, credentials, exposed headers, and max-age.
Header injection/removal, path rewriting (strip/add prefix), security headers, and JSON response envelope wrapping.
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