Skip to content

Run a HTTP REST server that serves JSON content from files

Notifications You must be signed in to change notification settings

rlindooren/simple-rest-json-server

Repository files navigation

Simple REST JSON server

Run a HTTP REST server that serves JSON* content from files.

* other file types work as well, the content-type is determined by the file extension.

It's a couple of lines of Kotlin code, using Ktor and Clikt.

Index page

Json response

If you need a more advanced mock REST JSON server, then please have a look at JSON Server.


Usage

Provide REST path and file mapping 'routes' as arguments, e.g.:

--route "/api/products/=/data/allProducts.json"
--route "/api/customers/=/data/allCustomers.json"
etc.

Run with Docker 🐳

A pre-build image can be found here: rlindooren/simple-rest-json-server:latest

# Update this to match your own set-up
DATA_DIR="$(pwd)/example-data/"
PORT=8080

docker run \
  --rm \
  --volume "${DATA_DIR}:/data/" \
  -p "${PORT}:${PORT}" \
  --env "PORT=${PORT}" \
  rlindooren/simple-rest-json-server:latest \
  --route "/api/products/=/data/allProducts.json" \
  --route "/api/customers/=/data/allCustomers.json"

Then goto http://localhost:8080/.

Run with Gradle

./gradlew run --args="--route /api/products/=example-data/allProducts.json --route /api/customers/=example-data/allCustomers.json"

Build Docker image

./gradlew installDist

docker build -t simple-rest-json-server .
# Update this to match your own set-up
DATA_DIR="$(pwd)/example-data/"
PORT=8080

docker run \
  --rm \
  --volume "${DATA_DIR}:/data/" \
  -p "${PORT}:${PORT}" \
  --env "PORT=${PORT}" \
  simple-rest-json-server \
  --route "/api/products/=/data/allProducts.json" \
  --route "/api/customers/=/data/allCustomers.json"

When serving really large files you may have to extend the available memory(?). For example like so:

docker run \
  --env "JAVA_OPTS=-Xmx2024M" \
  --memory=2g \
  ...