Skip to content

Commit 2284f5d

Browse files
committed
v0.2.0
1 parent 7c28e3e commit 2284f5d

File tree

3 files changed

+379
-52
lines changed

3 files changed

+379
-52
lines changed

README.md

Lines changed: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# demo-api
22

3-
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.
3+
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.
4+
5+
## Usage
46

57
Create a JSON file called `data.json`:
68

@@ -19,27 +21,115 @@ Create a JSON file called `data.json`:
1921
}
2022
```
2123

22-
Then run `demo-api`.
24+
Then run `demo-api`. The server starts up.
2325

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

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

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

36+
37+
Anything that doesn't match returns a `404` status code.
38+
39+
## Examples with `curl`
40+
41+
Given the following data file:
42+
43+
44+
```json
45+
{
46+
"notes" : [
47+
{
48+
"id" : 1,
49+
"title" : "Hello World"
50+
},
51+
{
52+
"id" : 2,
53+
"title" : "The second note"
54+
}
55+
]
56+
}
57+
```
58+
59+
To get everything:
60+
61+
```
62+
curl -i localhost:8080/
63+
```
64+
65+
To get the `notes` node:
66+
67+
68+
```
69+
curl -i localhost:8080/notes
70+
```
71+
72+
To get the `notes/1` node:
73+
74+
75+
```
76+
curl -i localhost:8080/notes/1
77+
```
78+
79+
To add a new note:
80+
81+
```
82+
curl -i -X POST http://localhost:8080/notes \
83+
-H "Content-type: application/json" \
84+
-d '{"title": "This is another note"}'
85+
```
86+
87+
To update the contents of the first note:
88+
89+
```
90+
curl -i -X PUT http://localhost:3000/notes/1 \
91+
-H "Content-type: application/json" \
92+
-d '{"title": "This is the third note"}'
93+
```
94+
95+
To delete the third note:
96+
97+
```
98+
curl -i -X DELETE localhost:3000/notes/3
99+
```
100+
101+
If you use a different JSON file, your paths will be different.
102+
103+
### Advanced Usage
104+
105+
To specify a different port, use the `-p` option:
106+
107+
```
108+
demo-api -p 4000
109+
```
110+
111+
To specify a different filename, in case you don't like `data.json` as the default, use `-f` and specify the file:
112+
113+
114+
```
115+
demo-api -f notes.json
116+
```
117+
118+
To view the version, use `-v`:
119+
120+
```
121+
demo-api -v
122+
```
123+
33124
## Installation
34125

35126
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`.
36127

37128

38129
## Roadmap
39130

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

44134
## Contributing
45135

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

58148
```
59-
$ gin go run app.go
149+
$ gin --appPort 8080 go run app.go
60150
```
61151

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

64-
Make changes, create a PR.
154+
Make changes, run the tests, create a PR.
65155

66156
## History
67157

68-
* 2018-09-02 - v0.2.0
158+
* 2018-09-03 - v0.3.0
69159
* Refactoring to make testing possible
70160
* Adds test suite
161+
* Pretty print
162+
* Supports `PUT` and `PATCH`
71163
* Supports `-v` option to show version
72164
* Supports `-f` option to specify the data file
73165
* Supports `-p` option to specify the port

0 commit comments

Comments
 (0)