/restinthemiddle

HTTP logging proxy

Primary LanguageGo

restinthemiddle

This Go program acts as a lightweight HTTP logging proxy for developing and staging environments. If you put it between an API client and the API you can easily monitor requests and responses.

Installation

Docker (recommended)

Pull the Docker image from Docker Hub

docker pull jdschulze/restinthemiddle

Build the Docker image yourself

Clone this repository and run the build script.

git clone https://github.com/jensschulze/restinthemiddle.git
cd restinthemiddle
./build

Build the binary yourself

Clone this repository and run go build.

git clone https://github.com/jensschulze/restinthemiddle.git
cd restinthemiddle
go build -o restinthemiddle

Usage

Typically you place the logging proxy between an application and an API:

+-----------------+         +-----------------+         +-----------------+
|                 +-------->+                 +-------->+                 |
|   Application   |         | Restinthemiddle |         |       API       |
|                 +<--------+                 +<--------+                 |
+-----------------+         +-----------------+         +-----------------+

But there are cases where it makes sense to place it between your browser and the application. For example you could want to add custom headers to every request (kind of an off-label use, because no logging is needed):

+-----------------+         +-----------------+         +-----------------+
|                 +-------->+                 +-------->+                 |
|     Browser     |         | Restinthemiddle |         |   Application   |
|                 +<--------+                 +<--------+                 |
+-----------------+         +-----------------+         +-----------------+

Configuration

Restinthemiddle is intended for use in a dockerized environment. Therefore it is configurable entirely via environment variables.

The ascending order of precedence (last wins) is:

  • Configuration via file (not implemented yet)
  • Configuration via CONFIG environment variable
  • Any other Environment variables

Environment variables

  • TARGET_HOST_DSN (required): The DSN of the target host in the form schema://username:password@hostname:port/basepath?query.
    • schema (required) is http or https
    • username:password@ is optional and will be evaluated only if both values are set.
    • hostname (required)
    • port is optional. Standard ports are 80 (http) and 443 (https).
    • basepath is optional. Will be prefixed to any request URL path pointed at Restinthemiddle. See examples section.
    • query is optional. If set, query will precede the actual request’s query.
  • PORT (optional): The port on which Restinthemiddle will be listening to requests. Defaults to 8000.
  • LOGGING_ENABLED (optional): Defaults to true.
  • CONFIG (optional): At the moment you can configure extra headers and logging as a JSON string in the form:
{
    "headers": {
        "X-App-Version": "3.0.0",
        "Another-Header": "Test"
    },
    "loggingEnabled": false
}

Examples

Basic

We want to log HTTP calls against www.example.com over an insecure connection.

# Set up the proxy
docker run -it --rm -e TARGET_HOST_DSN=http://www.example.com -p 8000:8000 jdschulze/restinthemiddle

# In another terminal window we make the API call against http://www.example.com/api/visitors
curl -i http://127.0.0.1:8000/api/visitors

Advanced

We want to log HTTP calls against www.example.com:4430 over a TLS connection (https://…). The API is protected by HTTP basic auth (username: user; password: pass). The base path always contains api/.

Note that we define a base path in TARGET_HOST_DSN that prefixes any subsequent calls!

# Set up the proxy
docker run -it --rm -e TARGET_HOST_DSN=https://user:pass@www.example.com:4430/api?start=1577833200 -p 8000:8000 jdschulze/restinthemiddle

# In another terminal window we make the API call against https://user:pass@www.example.com:4430/api/visitors?start=1577833200
curl -i http://127.0.0.1:8000/visitors

With configuration

We want to log HTTP calls against www.example.com over an insecure connection. Every request has to be enhanced with a custom header X-App-Version: 3.0.0. No logging shall take place.

# Set up the proxy
docker run -it --rm -e TARGET_HOST_DSN=http://www.example.com -e CONFIG='{"headers":{"X-App-Version":"3.0.0"},"loggingEnabled":false}' -p 8000:8000 jdschulze/restinthemiddle:latest

# In another terminal window we make the API call against http://www.example.com/home
curl -i http://127.0.0.1:8000/home