sim

command module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 17 Imported by: 0

README

Sim

Go goreleaser

Why

Make the dev loop crazy fast.

What

Sim is straight forward API simulation tools that's tiny and fast secure and scalable.

Most of today's API mocking tools run in virtual machines such as the JVM or NPM. Sim is written in Golang and leans on standard libraries:

It's orders of magnitude smaller binary and memory usage. Which much lower CPU usage. Each process can simulation multiple APIs. Running on Kubernetes? Three pods could simulate every API in your organization with high-availability.

Sim doesn't just mock APIs, it allows you to specify scripts for each API operation and back it with a simple disk storage.

Sim was written with extensive help from AI.

Install

Like jq, sim is a tiny (16Mb) standalone binary. You can download it from the releases page.

If you're on MacOS, you can use brew:

brew tap kitproj/sim --custom-remote https://github.com/kitproj/sim
brew install sim

Otherwise, you can use curl:

curl -q https://raw.githubusercontent.com/kitproj/sim/main/install.sh | sh

We do not support go install.

You can run a Docker image:

docker run --rm -ti -v my-directory:/apis kitproj/sim /sim /apis

Usage

Create a directory containing files named openapi.yaml.

Then run:

sim my-directory

Simulations are described by their API specification. For simple mocking, specify your examples in the OpenAPI spec:

openapi: 3.0.0
info:
  title: Hello API
  version: 1.0.0
servers:
  - url: http://localhost:8080
paths:
  /hello:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              example: { "message": "Hello, world!" }

Or complex scripting

openapi: 3.0.0
info:
  title: Teapot API
  version: 1.0.0
servers:
  - url: http://localhost:4040
paths:
  /teapot:
    get:
      x-sim-script: |
        response = {
           "status": 418,
           "headers": {
             "Teapot": request.queryParams.teapot
           },
           "body": { "message": "I'm a " + db.get("/teapot/name") }
         }
      responses:
        '200':
          description: OK

Reference

In you script you have access to the following:

request

The request, e.g.

{
  "method": "PUT",
  "path": "/documents/bar",
  "pathParams": {
    "id": "bar"
  },
  "queryParams": {
    "foo": "bar"
  },
  "headers": {
    "Content-Type": "application/json"
  },
  "body": {
    "baz": "qux"
  }
}

randomUuid()

Generates a random UUID.

db allows you to persist and access data:

// get an object, maybe null
db.get(key);
// put an object
db.put(key, object);
// delete an object
db.delete(key)
// return an array of all objects
db.list(keyPrefix);

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
db

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL