Skip to content

Commit 54962c0

Browse files
authored
Merge pull request #213 from pharindoko/feat/update-docs
Feat/update docs
2 parents 1d89646 + 266c68f commit 54962c0

File tree

2 files changed

+63
-173
lines changed

2 files changed

+63
-173
lines changed

README.md

Lines changed: 60 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
## Features
1818

19-
- Easily generate routes and resources for the Api via ([json-server](https://github.com/typicode/json-server))
20-
- **New:** Added Swagger UI support
19+
- Easily generate routes and resources for the Api via [json-server](https://github.com/typicode/json-server)
20+
- **New:** Added CLI
2121
- Deployment:
2222
- Deployed in AWS cloud within Minutes by a single command
2323
- Almost **zero costs** (First million requests for Lambda are free)
@@ -34,76 +34,72 @@
3434

3535
## Quickstart
3636

37-
### 1. Clone Solution
37+
### 1. Install Solution
3838

3939
```bash
40-
git clone https://github.com/pharindoko/json-serverless.git
41-
cd json-serverless
40+
npm i -g json-serverless
4241
```
4342

44-
### 2. Install dependencies
43+
### 2. Run local
44+
1. create a jsonserver-file sample e.g. db.json
4545

46-
```bash
47-
npm install -g serverless
48-
npm i
49-
```
46+
```
47+
{
48+
"posts": [
49+
{ "id": 1, "title": "json-server", "author": "typicode" },
50+
{ "id": 2, "title": "test", "author": "yourAuthor" }
51+
],
52+
"comments": [
53+
{ "id": 1, "body": "some comment", "postId": 1 }
54+
],
55+
"profile": { "name": "typicode" }
56+
}
57+
```
5058
51-
### 3. Verify AWS Access / Credentials
5259
53-
=> You need to have access to AWS to upload the solution.
60+
2. execute command
5461
55-
```bash
56-
aws sts get-caller-identity
57-
```
62+
```
63+
jsonsls run db.json
64+
```
5865
59-
### 4. Update db.json file in root directory
6066
61-
- Root properties marked in **bold** are the generated endpoints of the API ([route generation and json validation is done via json-server](https://github.com/typicode/json-server))
67+
### 3. Deploy api to AWS
6268
63-
<pre><code>
64-
{
65-
<b>"posts"</b>: [
66-
{ "id": 1, "title": "json-server", "author": "typicode" }
67-
],
68-
<b>"comments"</b>: [
69-
{ "id": 1, "body": "some comment", "postId": 1 }
70-
],
71-
<b>"profile"</b>: { "name": "typicode" }
72-
}
73-
</code></pre>
69+
1. Verify that you have a AWS account and set appropriate credentials
70+
2. execute command
7471
75-
### 5. Deploy via Serverless Framework
72+
```bash
73+
jsonsls create-stack db.json {optional: STAGE}
74+
```
7675
77-
```bash
78-
# set --stage parameter for different stages
79-
serverless deploy --stage dev
80-
```
76+
- a stack template folder will be created that contains the deployable serverless framework solution.
77+
<b>You can use the serverless cli in this stack template folder.</b>
78+
79+
80+
81+
- When the deployment was successful you can see following output
82+
83+
<pre>
84+
<code>
85+
Service Information
86+
service: serverless-json-server
87+
stage: dev
88+
region: eu-central-1
89+
stack: serverless-json-server-dev
90+
api keys:
91+
serverless-json-server.dev: <b>{API-KEY}</b>
92+
endpoints:
93+
ANY - <b>https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/ <== {ENDPOINTURL}</b>
94+
ANY - https://xxxxxxx.eu-central-1.amazonaws.com/dev/{proxy+}
95+
functions:
96+
app: serverless-json-server-dev-app
97+
layers:
98+
None
99+
Serverless: Removing old service artifacts from S3...
100+
</pre></code>
81101
82-
- serverless-webpack is used
83-
- the build will be triggered automatically
84-
85-
### 6. When the deployment with serverless framework was successful you can see following output
86-
87-
<pre>
88-
<code>
89-
Service Information
90-
service: serverless-json-server
91-
stage: dev
92-
region: eu-central-1
93-
stack: serverless-json-server-dev
94-
api keys:
95-
serverless-json-server.dev: <b>{API-KEY}</b>
96-
endpoints:
97-
ANY - <b>https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/ <== {ENDPOINTURL}</b>
98-
ANY - https://xxxxxxx.eu-central-1.amazonaws.com/dev/{proxy+}
99-
functions:
100-
app: serverless-json-server-dev-app
101-
layers:
102-
None
103-
Serverless: Removing old service artifacts from S3...
104-
</pre></code>
105-
106-
### 7. Test your Api
102+
### 4. Test your Api
107103
108104
#### With Swagger
109105
@@ -142,7 +138,7 @@ What`s my {route} ? -> see [json-server documentation](https://github.com/typico
142138
2. re-deploy the stack via serverless framework
143139

144140
```bash
145-
sls deploy
141+
jsonsls update-stack
146142
```
147143

148144
3. delete db.json file in S3 Bucket
@@ -159,18 +155,13 @@ curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" https://xxxxx
159155

160156
=> With the next request a new db.json file will be created in the S3 Bucket
161157

162-
#### Change Stackname
163-
164-
[edit service property in serverless.yml (in root directory)](https://github.com/pharindoko/json-server-less-lambda/blob/66756961d960c44cf317ca307b097f595799a890/serverless.yml#L8)
165-
166-
#### Adapt settings in config/appconfig.yml file
158+
#### Adapt settings
167159

168160
| Attribute | Description | Type | Default |
169161
|---|---|---|---|
170162
| readOnly | Make API readonly - all API - write operations are forbidden (http 403)) | string |false |
171163
| enableSwagger | Enable swagger and swagger UI support | string | true |
172164
| enableApiKeyAuth | Make your routes private by using an additional ApiKey | boolean | false |
173-
| jsonFile | path of json file that will be used | string | db.json |
174165
| enableJSONValidation | validate JSON file at start | boolean | true |
175166

176167
## Used Packages
@@ -187,22 +178,14 @@ curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" https://xxxxx
187178
- [AWS Lambda](https://aws.amazon.com/lambda/features/)
188179
- [AWS S3](https://aws.amazon.com/s3/)
189180

190-
## Develop and debug locally
181+
## Further Development
191182

192-
db.json file will be loaded directly from your local filesystem. No AWS access is needed.
183+
the json file will be loaded directly from your local filesystem. No AWS access is needed.
193184

194185
### Start solution
195186

196187
```bash
197-
npm run start
198-
```
199-
200-
### Debug solution
201-
202-
If you want to debug locally in VS Code everything is already setup (using webpack with sourcemap support)
203-
204-
```bash
205-
npm run debug
188+
make start-test
206189
```
207190

208191
### Test your API
@@ -229,101 +212,8 @@ curl -H "Content-Type: application/json" http://localhost:3000/api/{route}
229212

230213
What`s my {route} ? -> see [json-server documentation](https://github.com/typicode/json-server)
231214

232-
## Develop locally with cloud resources
233-
234-
Use same components (S3, LowDB) as the lambda does but have code executed locally.
235-
236-
### 1. Add .env file to root folder
237-
238-
**Mind:** If you haven`t deployed the solution yet, please create a private S3-Bucket and .json - file manually or deploy the solution first to AWS via serverless framework<br>
239-
**Mind:** This function requires that you have access to AWS (e.g. via credentials)
240-
241-
- Copy the .env file from .env.sample in the root folder
242-
243-
```bash
244-
cp .env.sample .env
245-
```
246-
247-
- Required: Adapt settings in .env file
248-
249-
| Attribute | Description | Type | Default |
250-
|---|---|---|---|
251-
| S3File | JSON file used as db to read and write (will be created with a default json value - customize in db.json) | string |db.json |
252-
| S3Bucket | S3-Bucket - this bucket must already exist in AWS | string | json-server-less-lambda-dev |
253-
| readOnly | all API - write operations are forbidden (http 403)) | boolean | false |
254-
255-
#### 2. Start solution
256-
257-
```bash
258-
npm run dev
259-
```
260-
261-
#### 3. Test your API
262-
263-
#### With Swagger
264-
265-
Open the {ENDPOINTURL}: http://localhost:3000/ that you received as output
266-
267-
#### With Curl
268-
269-
1. replace the url with the url provided by serverless (see above)
270-
2. replace the {API - KEY} with the key you get from serverless (see above)
271-
3. replace {route} at the end of the url e.g. with posts (default value)
272-
273-
Default Schema:
274-
275-
```bash
276-
Default route is posts: (see db.json)
277-
curl -H "Content-Type: application/json" http://localhost:3000/api/posts
278-
279-
# or another route given in db.json file
280-
curl -H "Content-Type: application/json" http://localhost:3000/api/{route}
281-
282-
# with enableApiKeyAuth=true
283-
curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}
284-
285-
```
286-
287-
What`s my {route} ? -> see [json-server documentation](https://github.com/typicode/json-server)
288-
289-
## Diagnose issues
290-
291-
serverless-offline will help you to troubleshoot issues with the lambda execution in a fast manner.
292-
293-
**Mind:** The assumption is that the solution has been already deployed<br>
294-
**Mind:** This function requires that you have access to AWS (e.g. via credentials)
295-
296-
### 1. build sources and execute serverless offline
297-
298-
- sources will be build with typescript (tsc) in advance to test the functionality.
299-
- after that sls offline will be started
300-
301-
<pre><code>npm run diagnostic
302-
303-
<u>Result:</u>
304-
Serverless: Starting Offline: dev/eu-central-1.
305-
Serverless: Key with token: <b>{API-KEY}</b>
306-
Serverless: Remember to use <b>x-api-key</b> on the request headers
307-
308-
```
309-
</pre></code>
310-
311-
#### 2. make api calls
312-
313-
- Use a new terminal window and start to make api calls.
314-
- Replace {API-KEY} with the api key in the sls offline output (see above).
315-
- Replace {route} with the route you want to test e.g. /posts
316-
317-
<pre><code>
318-
curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" http://localhost:3000/api/{route}
319-
</pre></code>
320-
321215
## FAQ
322216

323-
### How can I change the lambda region or stack name
324-
325-
Please have a look to the serverless guideline: https://serverless.com/framework/docs/providers/aws/guide/deploying/
326-
327217
### Cannot use Swagger UI when enableApiKeyAuth is true
328218

329219
The apiKey is set in AWS API Gateway. This means all requests (even the standard route) need to use the API-KEY.
@@ -336,7 +226,7 @@ If you want to see the Swagger UI you need to add a plugin e.g. ModHeader to Chr
336226

337227
### I forgot the API-KEY I have set
338228

339-
Ensure you have credentials for AWS set.
229+
Ensure you have credentials for AWS set.
340230

341231
```bash
342232
sls info

packages/cli/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ USAGE
3535

3636
## `jsonsls create-stack FILE [STAGE]`
3737

38-
describe the command here
38+
create the stack in AWS.
3939

4040
```
4141
USAGE
@@ -71,7 +71,7 @@ _See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v2.2.3
7171

7272
## `jsonsls run FILE`
7373

74-
describe the command here
74+
run solution locally.
7575

7676
```
7777
USAGE
@@ -87,7 +87,7 @@ OPTIONS
8787

8888
## `jsonsls update-stack`
8989

90-
describe the command here
90+
update the stack in AWS.
9191

9292
```
9393
USAGE

0 commit comments

Comments
 (0)