How fbonareis/goexpert-ratelimiter works
Project type and purpose
• This is a Go-based backend microservice whose sole responsibility is to expose a simple HTTP API and enforce rate limiting per IP or API key, backed by Redis.
• The README confirms it’s intended to run in Docker (via docker-compose) alongside a Redis container, serving a /sample endpoint with rate-limit middleware.
File structure
• Top level:
• cmd/ratelimiter/main.go → entry point for the service
• Dockerfile & docker-compose.yaml → container build and orchestration
• internal/infra/… → contains all infrastructure code (configs, rate-limit implementation, webserver & middleware)
• test/ → integration or API tests against the running service
• Patterns & architectural hints:
• Separation of concerns under internal/infra:
– configs (configuration loader)
– ratelimit (core rate limiter interface + Redis implementation)
– web (HTTP server, handlers, middleware)
• A single process that hosts the webserver and middleware; Redis is external.
README for additional insights
• Environment variables define Redis host/credentials and two rate-limit thresholds (per IP, per token).
• The application exposes only one endpoint for demonstration (/sample), protected by the rate-limit middleware layer.
• Execution instructions confirm the service + Redis run in Docker.
a. Main components:
• Client(s) (e.g., curl, browser, mobile app)
• Go Rate-Limiter Service
– HTTP Server (Router + /sample handler)
– Middleware Layer (rate limiter)
– Configuration Loader
– Rate Limiter Core (interface)
– Redis Limiter Implementation
• Redis Cache/Store (as external service)
• Docker + Docker-Compose orchestration (optional container boundary)
b. Relationships & interactions:
– Client → HTTP Server: incoming request
– HTTP Server → Middleware: intercept request before handler
– Middleware → Rate Limiter Core → Redis Limiter → Redis: check/increment tokens
– Middleware → HTTP Server: either pass through or reject (429)
– Handler → Response → Client
– The service reads configuration at startup (Env loader → config.go)
c. Architectural patterns & principles:
– Layered architecture: Handler ↔ Middleware ↔ Core ↔ Infra (Redis)
– Dependency inversion: rate limiter interface decouples core logic from Redis implementation
– Single responsibility: config loader, rate‐limit logic, webserver all separated
d. Key technologies/frameworks:
– Language: Go
– HTTP framework: net/http (custom webserver under internal/infra/web/webserver)
– Redis client (library used in redis_limiter.go)
– Docker & Docker-Compose for containerization
Detailed capture of essential elements
• Do not subdivide too granularly—focus on the five major components: Client, HTTP Server, Middleware, RedisLimiter, Redis.
• Middleware with the two rate-limit rules (per IP & per token) and config source.
• Optionally, show the environment‐variable config file feeding into the configuration loader.