genmock

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: MIT Imports: 12 Imported by: 0

README

💻 Gen Mockserver

gen-mockserver is a simple tool for generating a mock server based on an OpenAPI Spec v2 or v3.

🛠️ Installation

go install github.com/bramca/gen-mockserver/cmd/genmock@latest or check out the releases

🎉 Usage

go run genmock.go -specfile <path to openapi spec> -specversion <openapi spec version> [-scheme <http (default)|https>] [-port <5000 (default)] [-dbfile <db.json (default)>] [-serverfile <server.js (default)>] [-recursiondepth <0 (default)>] [-exampledata <false (default)>]

Options
  • -specfile, -s
    • path to your openapi specification file

  • -specversion, -v
    • specify the major version of your spec
    • values: 2, 3

  • -scheme, -c [optional]
    • specify the scheme that should be used by the mock server
    • values: http (default), https

  • -port, -p [optional]
    • specify the port that should be used by the mock server
    • values: 5000 (default)

  • -dbfile, -d [optional]
    • filename for the generated database (use the .json file extension)
    • values: db.json (default)

  • -serverfile, -f [optional]
    • filename for the generated server (use the .js file extension)
    • values: server.js (default)

  • -recursiondepth, -r [optional]
    • give the maximum recursion depth to generate the response json (default 0)
    • values: 0 (default)

  • -exampledata, -e [optional]
    • generate fake example data in the responses
    • values: false (default), true
Example

genmock -s openapi.yaml -v 3 -e true -r 1

This will generate the following files:

.
├── Dockerfile
├── compose.yaml
├── db.json
├── package.json
└── server.js
Files
  • Dockerfile
    • for building a docker container running the server

  • compose.yaml
    • to run docker compose command creating the container

  • db.json
    • a database file where you can store mock/example data

  • package.json
    • npm packages the server depends on

  • server.js
    • the file that is run when starting the server
    • all the routes are in here with there different operations on them
    • this serves a bit as a template, it will return empty results unless you set the -e flag to true
    • you can define some logic for every route as you wish in this file
Run the server

npm install ; npm start
or
docker compose up --build [-d]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateDocker

func GenerateDocker(dbFileName string, serverFile string, port int, scheme string)

func GeneratePackageJson

func GeneratePackageJson(serverFile string)

func GenerateServerFile

func GenerateServerFile(scheme string, port int, dbFilename string, serverFilename string, featureFileDataStructure map[string]map[string][]RequestStructure)

func RandStringBytesRmndr

func RandStringBytesRmndr(n int) string

func SpecV2toRequestStructureMap

func SpecV2toRequestStructureMap(specFilename string, maxRecursionDepth int, genExamples bool) map[string]map[string][]RequestStructure

func SpecV3toRequestStructureMap

func SpecV3toRequestStructureMap(specFilename string, maxRecursionDepth int, genExamples bool) map[string]map[string][]RequestStructure

Types

type RequestStructure

type RequestStructure struct {
	Path          string
	Method        string
	Body          string
	DbEntry       string
	ResponseCode  string
	ResponseBody  map[string]any
	RequestParams []string
	RequestBody   map[string]any
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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