Skip to content

Commit c8c0242

Browse files
authored
Merge pull request #83 from pharindoko/feature/generate-swagger
Feature/generate swagger
2 parents 45203ec + f4b476c commit c8c0242

19 files changed

+955
-106
lines changed

.env.sample

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
S3FILE=db.json
2-
S3BUCKET=json-serverless-dev
3-
READONLY=false
1+
S3File=db.json
2+
S3Bucket=json-serverless-dev
3+
readOnly=false

README.md

Lines changed: 130 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,53 @@
99
- [Develop locally with cloud resources](#develop-locally-with-cloud-resources)
1010
- [Diagnose issues](#diagnose-issues)
1111

12+
## Architecture
13+
14+
![Architecture](docs/json-serverless.png)
15+
1216
## Features
1317

14-
- Development:
15-
- Easily setup routes and resources for the REST Api via json file. [(via json-server)](https://github.com/typicode/json-server)
16-
- This solution written in **NodeJS** can be easily extended for additional enhanced scenarios
17-
- adding user authentication
18-
- own custom domain
19-
- additional routes etc.
20-
- Develop and test solution locally in Visual Studio Code
21-
- Security: This Api is secured via API Key and https by default.
18+
- Easily setup routes and resources for the REST Api via json file. [(via json-server)](https://github.com/typicode/json-server)
19+
- **New:** Added Swagger UI support
2220
- Deployment:
2321
- Deployed in AWS cloud within Minutes by a single command
2422
- Almost **zero costs** (First million requests for Lambda are free)
2523
- Less maintenance as the deployed solution runs **serverless**
24+
- Security:
25+
- Secured with https by default.
26+
- Optional: Use a generated API Key
27+
- Customization:
28+
- This solution written in **NodeJS** can be easily extended for additional enhanced scenarios
29+
- adding user authentication
30+
- own custom domain
31+
- additional routes etc.
32+
- Develop and debug solution locally in Visual Studio Code
2633

2734
## Quickstart
2835

29-
##### 1. Clone Solution
36+
### 1. Clone Solution
3037

3138
```bash
32-
git clone https://github.com/pharindoko/json-serverless.git
39+
git clone https://github.com/pharindoko/json-serverless.git
3340
cd json-serverless
3441
```
3542

36-
##### 2. Install dependencies
43+
### 2. Install dependencies
3744

3845
```bash
3946
npm install -g serverless
4047
npm i
4148
```
4249

43-
##### 3. Verify AWS Access / Credentials
50+
### 3. Verify AWS Access / Credentials
4451

4552
=> You need to have access to AWS to upload the solution.
4653

4754
```bash
4855
aws sts get-caller-identity
4956
```
5057

51-
##### 4. Update db.json file in root directory
58+
### 4. Update db.json file in root directory
5259

5360
- Childproperties are the REST endpoints you create
5461
- Samplefile: Routes marked **bold**
@@ -61,8 +68,7 @@ aws sts get-caller-identity
6168
}
6269
</code></pre>
6370

64-
##### 5. Deploy via Serverless Framework
65-
71+
### 5. Deploy via Serverless Framework
6672

6773
```bash
6874
# set --stage parameter for different stages
@@ -72,7 +78,7 @@ serverless deploy --stage dev
7278
- serverless-webpack is used
7379
- the build will be triggered automatically
7480

75-
##### 6. When the deployment with serverless framework was successful you can see following output
81+
### 6. When the deployment with serverless framework was successful you can see following output
7682

7783
<pre>
7884
<code>
@@ -82,9 +88,9 @@ stage: dev
8288
region: eu-central-1
8389
stack: serverless-json-server-dev
8490
api keys:
85-
serverless-json-server.dev: <b>{API - KEY}</b>
91+
serverless-json-server.dev: <b>{API-KEY}</b>
8692
endpoints:
87-
ANY - <b>https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/</b>
93+
ANY - <b>https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/ <== {ENDPOINTURL}</b>
8894
ANY - https://xxxxxxx.eu-central-1.amazonaws.com/dev/{proxy+}
8995
functions:
9096
app: serverless-json-server-dev-app
@@ -93,45 +99,40 @@ layers:
9399
Serverless: Removing old service artifacts from S3...
94100
</pre></code>
95101

96-
##### 7. Test your Api
102+
### 7. Test your Api
103+
104+
#### With Swagger
105+
106+
Open the {ENDPOINTURL}: https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/ that you received as output
97107

98-
##### With Curl
108+
**MIND**: If you have set enableApiKeyAuth to true => [SwaggerUI](#Cannot-use-Swagger-UI-when-enableApiKeyAuth-is-true)
109+
)
110+
111+
#### With Curl
99112

100113
1. replace the url with the url provided by serverless (see above)
101-
2. replace the {API - KEY} with the key you get from serverless (see above)
114+
2. replace the {API-KEY} with the key you get from serverless (see above)
102115
3. replace {route} at the end of the url e.g. with posts (default value)
103116

104117
Default Schema:
105118

106119
```bash
107120
Default route is posts: (see db.json)
108-
curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/posts
121+
curl -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/posts
109122

110-
#or another route given in db.json file
111-
curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/{route}
112-
```
123+
# or another route given in db.json file
124+
curl -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}
113125

114-
##### With Postman
126+
# with enableApiKeyAuth=true
127+
curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}
115128

116-
- Create a new GET Request and add these values to the header section
117-
118-
|Key| Value|
119-
|---|---|
120-
|x-api-key | {API - KEY}|
121-
|Content-Type | application/json|
122-
123-
- Enter as Url the endpoints url
124-
125-
```bash
126-
https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/{route}
127-
# e.g. default value: https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/posts
128129
```
129130

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

132133
## Customization
133134

134-
#### Update content of db.json
135+
### Update content of db.json
135136

136137
1. update local db.json file in root directory with new values
137138
2. re-deploy the stack via serverless framework
@@ -141,10 +142,15 @@ What`s my {route} ? -> see [json-server documentation](https://github.com/typico
141142
```
142143

143144
3. delete db.json file in S3 Bucket
144-
4. Make a GET request against the root url https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/
145+
4. Make a GET request against the root url https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api
145146

146147
```bash
147-
curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev
148+
curl -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api
149+
150+
# with enableApiKeyAuth=true
151+
curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}
152+
153+
148154
```
149155

150156
=> With the next request a new db.json file will be created in the S3 Bucket
@@ -153,13 +159,13 @@ curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxx
153159

154160
[edit service property in serverless.yml (in root directory)](https://github.com/pharindoko/json-server-less-lambda/blob/66756961d960c44cf317ca307b097f595799a890/serverless.yml#L8)
155161

156-
#### Adapt settings in config/servleressconfig.yml file
162+
#### Adapt settings in config/appconfig.yml file
157163

158164
| Attribute | Description | Type | Default |
159165
|---|---|---|---|
160-
| 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 |
161-
| S3BUCKET | S3-Bucket - this bucket must already exist in AWS | string | json-server-less-lambda-dev |
162-
| READONLY | all API - write operations are forbidden (http 403)) | boolean | false |
166+
| readOnly | Make API readonly - all API - write operations are forbidden (http 403)) | string |false |
167+
| enableSwagger | Enable swagger and swagger UI support | string | true |
168+
| enableApiKeyAuth | Make your routes private by using an additional ApiKey | boolean | false |
163169

164170
## Used Packages
165171

@@ -170,7 +176,7 @@ curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxx
170176

171177
## Components
172178

173-
- [NodeJS 8.10](https://nodejs.org/en/about/)
179+
- [NodeJS 8.10](https://nodejs.org/en/about/)
174180
- [AWS API Gateway](https://aws.amazon.com/api-gateway/)
175181
- [AWS Lambda](https://aws.amazon.com/lambda/features/)
176182
- [AWS S3](https://aws.amazon.com/s3/)
@@ -179,13 +185,14 @@ curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxx
179185

180186
db.json file will be loaded directly from your local filesystem. No AWS access is needed.
181187

182-
#### Start solution
188+
### Start solution
183189

184190
```bash
185191
npm run start
186192
```
187193

188-
#### Debug solution
194+
### Debug solution
195+
189196
If you want to debug locally in VS Code everything is already setup (using webpack with sourcemap support)
190197

191198
```bash
@@ -194,13 +201,24 @@ npm run debug
194201

195202
#### 2. Test your API
196203

197-
To test you can use e.g. [Postman](https://www.getpostman.com/)
204+
#### With Swagger
198205

199-
- Open Postman
200-
- Enter as Url the endpoints url
206+
Open the {ENDPOINTURL}: http://localhost:3000/ that you received as output
207+
208+
#### With Curl
209+
210+
1. replace the url with the url provided by serverless (see above)
211+
2. replace the {API - KEY} with the key you get from serverless (see above)
212+
3. replace {route} at the end of the url e.g. with posts (default value)
213+
214+
Default Schema:
201215

202216
```bash
203-
https://localhost:3000/{route} #e.g. default value: https://localhost:3000/posts/
217+
Default route is posts: (see db.json)
218+
curl -H "Content-Type: application/json" http://localhost:3000/api/posts
219+
220+
#or another route given in db.json file
221+
curl -H "Content-Type: application/json" http://localhost:3000/api/{route}
204222
```
205223

206224
What`s my {route} ? -> see [json-server documentation](https://github.com/typicode/json-server)
@@ -209,7 +227,7 @@ What`s my {route} ? -> see [json-server documentation](https://github.com/typico
209227

210228
Use same componentes (S3, LowDB) as the lambda does but have code executed locally.
211229

212-
#### 1. Add .env file to root folder
230+
### 1. Add .env file to root folder
213231

214232
**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>
215233
**Mind:** This function requires that you have access to AWS (e.g. via credentials)
@@ -224,9 +242,9 @@ cp .env.sample .env
224242

225243
| Attribute | Description | Type | Default |
226244
|---|---|---|---|
227-
| 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 |
228-
| S3BUCKET | S3-Bucket - this bucket must already exist in AWS | string | json-server-less-lambda-dev |
229-
| READONLY | all API - write operations are forbidden (http 403)) | boolean | false |
245+
| 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 |
246+
| S3Bucket | S3-Bucket - this bucket must already exist in AWS | string | json-server-less-lambda-dev |
247+
| readOnly | all API - write operations are forbidden (http 403)) | boolean | false |
230248

231249
#### 2. Start solution
232250

@@ -236,13 +254,28 @@ npm run dev
236254

237255
#### 3. Test your API
238256

239-
To test you can use e.g. [Postman](https://www.getpostman.com/)
257+
#### With Swagger
258+
259+
Open the {ENDPOINTURL}: http://localhost:3000/ that you received as output
260+
261+
#### With Curl
262+
263+
1. replace the url with the url provided by serverless (see above)
264+
2. replace the {API - KEY} with the key you get from serverless (see above)
265+
3. replace {route} at the end of the url e.g. with posts (default value)
240266

241-
- Open Postman
242-
- Enter as Url the endpoints url
267+
Default Schema:
243268

244269
```bash
245-
https://localhost:3000/{route} #e.g. default value: https://localhost:3000/posts
270+
Default route is posts: (see db.json)
271+
curl -H "Content-Type: application/json" http://localhost:3000/api/posts
272+
273+
# or another route given in db.json file
274+
curl -H "Content-Type: application/json" http://localhost:3000/api/{route}
275+
276+
# with enableApiKeyAuth=true
277+
curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}
278+
246279
```
247280

248281
What`s my {route} ? -> see [json-server documentation](https://github.com/typicode/json-server)
@@ -254,7 +287,7 @@ serverless-offline will help you to troubleshoot issues with the lambda executio
254287
**Mind:** The assumption is that the solution has been already deployed<br>
255288
**Mind:** This function requires that you have access to AWS (e.g. via credentials)
256289

257-
#### 1. build sources and execute serverless offline
290+
### 1. build sources and execute serverless offline
258291

259292
- sources will be build with babel in advance to test the functionality.
260293
- after that sls offline will be started
@@ -276,5 +309,39 @@ Serverless: Remember to use <b>x-api-key</b> on the request headers
276309
- Replace {route} with the route you want to test e.g. /posts
277310

278311
<pre><code>
279-
curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" http://localhost:3000/{route}
280-
</pre></code>
312+
curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" http://localhost:3000/api/{route}
313+
</pre></code>
314+
315+
## FAQ
316+
317+
### How can I change the lambda region or stack name
318+
319+
Please have a look to the serverless guideline: https://serverless.com/framework/docs/providers/aws/guide/deploying/
320+
321+
### Cannot use Swagger UI when enableApiKeyAuth is true
322+
323+
The apiKey is set in AWS API Gateway. This means all requests (even the standard route) need to use the API-KEY.
324+
325+
If you want to see the Swagger UI you need to add a plugin e.g. ModHeader to Chrome and add the needed headers:
326+
- Content-Type: application/json
327+
- x-api-key: {provided by sls info in the output after deployment}
328+
329+
![ModHeader](docs/header.png)
330+
331+
### I forgot the API-KEY I have set
332+
333+
Ensure you have credentials for AWS set.
334+
335+
```bash
336+
sls info
337+
```
338+
339+
### Destroy the stack in the cloud
340+
341+
```bash
342+
sls remove
343+
```
344+
345+
### I deployed the solution but I get back a http 500 error
346+
347+
Check Cloudwatch Logs in AWS - the issue should be describe there. Log has the same name as the stack that has been created.

config/appconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"readOnly": false,
3+
"enableSwagger": true,
4+
"enableApiKeyAuth": false
5+
}
6+

config/serverlessconfig.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"S3File": "db.json",
3+
"S3Bucket": "${self:service}-${self:provider.stage}",
4+
"basePath": "/${self:provider.stage}"
5+
}

config/serverlessconfig.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/header.png

17.5 KB
Loading

docs/json-serverless.png

25 KB
Loading

0 commit comments

Comments
 (0)