A simple, professional caching reverse proxy server written in Go. This project forwards HTTP requests to a configurable backend, caches JSON responses in Redis, and serves cached responses to improve performance. Includes a CLI flag to clear the cache.
- CLI flags for port, origin URL, and cache clearing
- Proxies requests to a specified backend
- Caches JSON responses in Redis for 10 minutes
- Serves cached responses if available
- Command to clear the entire cache
- On startup, the server reads CLI flags for configuration
- Incoming HTTP requests are proxied to the backend origin
- If a cached response exists in Redis, it is returned immediately
- If not, the request is forwarded, and the response is cached
- The
--cacheflag clears all Redis cache entries
- Go installed
- Redis server running at
localhost:6379
# Install dependencies
go mod tidy
# Run the server (default port 3000, default origin)
go run myapp/main.go --port=3000 --origin=https://your-backend.comSend HTTP requests to http://localhost:3000/<path>. The proxy forwards the request to the specified origin and caches the response.
To clear all cached responses:
go run myapp/main.go --cache=true--port: Port to run the proxy server (default: 3000)--origin: Origin URL to proxy requests to (default: https://fake-json-api.mock.beeceptor.com)--cache: If true, clears all Redis cache and exits
Suppose you want to proxy requests to an example API and cache product data:
-
Start the server:
go run myapp/main.go --port=3000 --origin=https://api.example.com
-
Make a request:
curl http://localhost:3000/products
- The first request fetches data from
https://api.example.com/productsand caches it. - Subsequent requests to the same endpoint are served from cache (if within 10 minutes).
- The first request fetches data from
-
Clear the cache:
go run myapp/main.go --cache=true
- This command clears all cached responses in Redis.