Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
napcs committed Sep 3, 2018
1 parent 7c28e3e commit 2284f5d
Show file tree
Hide file tree
Showing 3 changed files with 379 additions and 52 deletions.
114 changes: 103 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# demo-api

A simple command-line JSON server. If you need to stub out an API locally, this is the tool you need. It's lightweight, works on Mac, Windows, and Linux, and requires no setup.
A simple command-line JSON server that also emulates a REST API. If you need to stub out an API locally, this is the tool you need. It's lightweight, works on Mac, Windows, and Linux, and requires no setup other than a JSON data file.

## Usage

Create a JSON file called `data.json`:

Expand All @@ -19,27 +21,115 @@ Create a JSON file called `data.json`:
}
```

Then run `demo-api`.
Then run `demo-api`. The server starts up.

Visit `http://localhost:8080` in the browser to see the file served as JSON.
Visit `http://localhost:8080` in the browser to serve the entire file as a JSON response.

In addition, since the JSON file is structured like a typical REST API data structure, the following will work:
In addition, since the JSON file is structured like a typical REST API data structure, the following endpoints will work:

* `GET http://localhost:8080/notes` - retrive all "notes"
* `GET http://localhost:8080/notes/1` - retrive the note with the `id` of `1`
* `POST http://localhost:8080/notes/` - Create a new note. This modifies the `data.json` file.
* `POST http://localhost:8080/notes/` with JSON payload - Create a new note. This modifies the `data.json` file.
* `PUT/PATCH http://localhost:8080/notes/1` with JSON payload - update the note with the `id` of `1`. This modifies the `data.json` file.
* `DELETE http://localhost:8080/notes/1` - Delete the note with the `id` of `1`. This modifies the `data.json` file.


Anything that doesn't match returns a `404` status code.

## Examples with `curl`

Given the following data file:


```json
{
"notes" : [
{
"id" : 1,
"title" : "Hello World"
},
{
"id" : 2,
"title" : "The second note"
}
]
}
```

To get everything:

```
curl -i localhost:8080/
```

To get the `notes` node:


```
curl -i localhost:8080/notes
```

To get the `notes/1` node:


```
curl -i localhost:8080/notes/1
```

To add a new note:

```
curl -i -X POST http://localhost:8080/notes \
-H "Content-type: application/json" \
-d '{"title": "This is another note"}'
```

To update the contents of the first note:

```
curl -i -X PUT http://localhost:3000/notes/1 \
-H "Content-type: application/json" \
-d '{"title": "This is the third note"}'
```

To delete the third note:

```
curl -i -X DELETE localhost:3000/notes/3
```

If you use a different JSON file, your paths will be different.

### Advanced Usage

To specify a different port, use the `-p` option:

```
demo-api -p 4000
```

To specify a different filename, in case you don't like `data.json` as the default, use `-f` and specify the file:


```
demo-api -f notes.json
```

To view the version, use `-v`:

```
demo-api -v
```

## Installation

To install, download the latest release to your system and copy the executable to a location on your path. Then launch it in a directory containing `data.json`.


## Roadmap

* `PUT/PATCH` support
* tests
* refactoring
* Refactoring. This code is a mess.
* A "no persist" mode - changes are accepted but not saved to the JSON file.

## Contributing

Expand All @@ -56,18 +146,20 @@ $ go get github.com/codegangsta/gin
Run development version:

```
$ gin go run app.go
$ gin --appPort 8080 go run app.go
```

The server is now listening on `localhost:3000` and will reload on code change.

Make changes, create a PR.
Make changes, run the tests, create a PR.

## History

* 2018-09-02 - v0.2.0
* 2018-09-03 - v0.3.0
* Refactoring to make testing possible
* Adds test suite
* Pretty print
* Supports `PUT` and `PATCH`
* Supports `-v` option to show version
* Supports `-f` option to specify the data file
* Supports `-p` option to specify the port
Expand Down
Loading

0 comments on commit 2284f5d

Please sign in to comment.