json-server

command module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: MIT Imports: 1 Imported by: 0

README

JSON Server

GoDoc codecov goreportcard

Create a dummy REST API from a json file with zero coding in seconds. Helps you to continue your development process when an API you depend on, doesn't exist or isn't complete yet.

Inspired from json-server javascript package.

Getting started

Get the package

go get github.com/chanioxaris/json-server

Create a db.json file with your desired data

{
  "posts": [
    { 
       "id": "1", 
       "title": "json-server", 
       "author": "chanioxaris" 
    }
  ],
   "books": [
     {
       "id": "1",
       "title": "Clean Code",
       "published": 2008,
       "author": "Robert Martin"
     },
     {
       "id": "2",
       "title": "Crime and punishment",
       "published": 1866,
       "author": "Fyodor Dostoevsky"
     }
   ]
}

Start JSON Server

go run main.go start

If you navigate to http://localhost:3000/posts/1, you will get

{ 
  "id": "1", 
  "title": "json-server", 
  "author": "chanioxaris" 
}

Routes

Based on the previous json file and for each resource, the below routes will be generated

GET     /<resource>
GET     /<resource>/:id
POST    /<resource>
PUT     /<resource>/:id
PATCH   /<resource>/:id
DELETE  /<resource>/:id

When doing requests, it's good to know that:

  • For POST requests any id value in the body will be honored, but only if not already taken.
  • For POST requests without id value in the body, a new one will be generated.
  • For PUT requests any id value in the body will be ignored, as id values are not mutable.
  • For PATCH requests any id value in the body will be ignored, as id values are not mutable.

Parameters

  • You can specify an alternative port with the flag -p or --port. Default value is 3000.

go run main.go start -p 4000

  • You can specify an alternative file with the flag -f or --file. Default value is db.json.

go run main.go start -f example.json

  • You can toggle http request logs with the flag -l or --logs. Default value is false.

go run main.go start -l

Contributing

Contributions to this project are welcomed. For more information see the contribution guidelines.

License

json-server is MIT licensed.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package cmd contains the functionality for the set of commands currently supported by the CLI.
Package cmd contains the functionality for the set of commands currently supported by the CLI.
internal
handler
Package handler contains the full set of handler functions and routes supported by the web api.
Package handler contains the full set of handler functions and routes supported by the web api.
handler/common
Package common contains the set of handler functions and routes which by default supported from the web api.
Package common contains the set of handler functions and routes which by default supported from the web api.
logger
Package logger contains custom settings regarding the log functionality.
Package logger contains custom settings regarding the log functionality.
storage
Package storage defines an interface which describes storage CRUD functionality.
Package storage defines an interface which describes storage CRUD functionality.
web
Package web provides http support functionality.
Package web provides http support functionality.
web/middleware
Package middleware provides middleware support for REST API server.
Package middleware provides middleware support for REST API server.

Jump to

Keyboard shortcuts

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