Skip to content

Caching reverse proxy server in Go. Proxies HTTP requests, caches JSON responses in Redis, and supports easy cache clearing via CLI.

Notifications You must be signed in to change notification settings

ginnysingh789/caching-proxy-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Caching Proxy Server

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.

Features

  • 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

How It Works

  • 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 --cache flag clears all Redis cache entries

Getting Started

Prerequisites

  • Go installed
  • Redis server running at localhost:6379

Installation & Usage

# Install dependencies
go mod tidy

# Run the server (default port 3000, default origin)
go run myapp/main.go --port=3000 --origin=https://your-backend.com

Proxy Requests

Send HTTP requests to http://localhost:3000/<path>. The proxy forwards the request to the specified origin and caches the response.

Clear Cache

To clear all cached responses:

go run myapp/main.go --cache=true

Configuration Options

  • --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

Example

Suppose you want to proxy requests to an example API and cache product data:

  1. Start the server:

    go run myapp/main.go --port=3000 --origin=https://api.example.com
  2. Make a request:

    curl http://localhost:3000/products
    • The first request fetches data from https://api.example.com/products and caches it.
    • Subsequent requests to the same endpoint are served from cache (if within 10 minutes).
  3. Clear the cache:

    go run myapp/main.go --cache=true
    • This command clears all cached responses in Redis.

About

Caching reverse proxy server in Go. Proxies HTTP requests, caches JSON responses in Redis, and supports easy cache clearing via CLI.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages