11# JSON Serverless [ ![ Renovate enabled] ( https://img.shields.io/badge/renovate-enabled-brightgreen.svg )] ( https://renovatebot.com/ ) [ ![ Build Status] ( https://travis-ci.org/pharindoko/json-serverless.svg?branch=master )] ( https://travis-ci.org/pharindoko/json-serverless ) [ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-yellow.svg )] ( https://opensource.org/licenses/MIT ) <a href =" https://codeclimate.com/github/pharindoko/json-serverless/maintainability " ><img src =" https://api.codeclimate.com/v1/badges/12f2aa333ec4e24b1ac9/maintainability " /></a >
22
3- - [ Features] ( #features )
4- - [ Quickstart] ( #quickstart )
5- - [ Customization] ( #customization )
6- - [ Used Packages] ( #used-packages )
7- - [ Components] ( #components )
8- - [ Develop locally] ( #develop-locally )
9- - [ Develop locally with cloud resources] ( #develop-locally-with-cloud-resources )
10- - [ Diagnose issues] ( #diagnose-issues )
3+ - [ Features] ( #features )
4+ - [ Quickstart] ( #quickstart )
5+ - [ Customization] ( #customization )
6+ - [ Used Packages] ( #used-packages )
7+ - [ Components] ( #components )
8+ - [ Develop locally] ( #develop-locally )
9+ - [ Develop locally with cloud resources] ( #develop-locally-with-cloud-resources )
10+ - [ Diagnose issues] ( #diagnose-issues )
1111
1212## Features
13+
1314- Development:
1415 - Easily setup routes and resources for the REST Api via json file. [ (via json-server)] ( https://github.com/typicode/json-server )
1516 - This solution written in ** NodeJS** can be easily extended for additional enhanced scenarios
16- * adding user authentication
17- * own custom domain
18- * additional routes etc.
17+ - adding user authentication
18+ - own custom domain
19+ - additional routes etc.
1920 - Develop and test solution locally in Visual Studio Code
2021- Security: This Api is secured via API Key and https by default.
21- - Deployment:
22+ - Deployment:
2223 - Deployed in AWS cloud within Minutes by a single command
2324 - Almost ** zero costs** (First million requests for Lambda are free)
2425 - Less maintenance as the deployed solution runs ** serverless**
2526
2627## Quickstart
28+
2729##### 1. Clone Solution
30+
2831``` bash
2932git clone https://github.com/pharindoko/json-serverless.git
3033cd json-serverless
3134```
3235
3336##### 2. Install dependencies
37+
3438``` bash
3539npm install -g serverless
3640npm i
3741```
3842
3943##### 3. Verify AWS Access / Credentials
44+
4045=> You need to have access to AWS to upload the solution.
46+
4147``` bash
4248aws sts get-caller-identity
4349```
50+
4451##### 4. Update db.json file in root directory
4552
4653- Childproperties are the REST endpoints you create
47- - Samplefile: Routes marked < b > bold</ b >
54+ - Samplefile: Routes marked ** bold**
4855
4956<pre ><code >
5057{
51- "<b >basic </b >": {
52- "hello ": "world"
53- }
58+ "<b >posts </b >": [
59+ { "id": 1, "title ": "json-server", "author": "typicode" }
60+ ]
5461}
5562</code ></pre >
5663
@@ -65,7 +72,8 @@ serverless deploy --stage dev
6572- serverless-webpack is used
6673- the build will be triggered automatically
6774
68- ##### 6. When the deployment with serverless framework was successful you can see following output:
75+ ##### 6. When the deployment with serverless framework was successful you can see following output
76+
6977<pre >
7078<code >
7179Service Information
@@ -86,55 +94,64 @@ Serverless: Removing old service artifacts from S3...
8694</pre ></code >
8795
8896##### 7. Test your Api
89- ##### With Curl:
97+
98+ ##### With Curl
9099
911001 . replace the url with the url provided by serverless (see above)
921012 . replace the {API - KEY} with the key you get from serverless (see above)
93- 3 . replace {route} at the end of the url e.g. with basic (default value)
102+ 3 . replace {route} at the end of the url e.g. with posts (default value)
94103
104+ Default Schema:
95105
96- ```
97- Basic Schema:
98- curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/{route}
99-
106+ ``` bash
107+ 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
100109
101- Default route is basic: (see db.json)
102- curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/basic
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}
103112```
104- ##### With Postman:
105113
106- * Create a new GET Request and add these values to the header section
114+ ##### With Postman
115+
116+ - Create a new GET Request and add these values to the header section
107117
108118 | Key| Value|
109119 | ---| ---|
110120 | x-api-key | {API - KEY}|
111121 | Content-Type | application/json|
112122
113- * Enter as Url the endpoints url
123+ - Enter as Url the endpoints url
114124
115- ``` bash
125+ ``` bash
116126 https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/{route}
117- # e.g. default value: https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/basic
127+ # e.g. default value: https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/posts
118128```
119- What`s my {route} ? -> see [ json-server documentation] ( https://github.com/typicode/json-server )
120-
121129
130+ What`s my {route} ? -> see [ json-server documentation] ( https://github.com/typicode/json-server )
122131
123132## Customization
124133
125134#### Update content of db.json
135+
1261361 . update local db.json file in root directory with new values
1271372 . re-deploy the stack via serverless framework
138+
128139 ``` bash
129140 sls deploy
130141 ```
142+
1311433 . 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+
146+ ``` 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+ ```
132149
133150=> With the next request a new db.json file will be created in the S3 Bucket
134151
135152#### Change Stackname
136- [ edit service property in serverless.yml (in root directory)] ( https://github.com/pharindoko/json-server-less-lambda/blob/66756961d960c44cf317ca307b097f595799a890/serverless.yml#L8 )
137153
154+ [ edit service property in serverless.yml (in root directory)] ( https://github.com/pharindoko/json-server-less-lambda/blob/66756961d960c44cf317ca307b097f595799a890/serverless.yml#L8 )
138155
139156#### Adapt settings in config/servleressconfig.yml file
140157
@@ -144,61 +161,59 @@ What`s my {route} ? -> see [json-server documentation](https://github.com/typico
144161| S3BUCKET | S3-Bucket - this bucket must already exist in AWS | string | json-server-less-lambda-dev |
145162| READONLY | all API - write operations are forbidden (http 403)) | boolean | false |
146163
147-
148-
149164## Used Packages
150- * [ json-server] ( https://github.com/typicode/json-server )
151- * [ serverless framework] ( https://serverless.com/ )
152- * [ serverless http] ( https://github.com/dougmoscrop/serverless-http )
153- * [ lowdb-adapter-aws-s3] ( https://github.com/nicekiwi/lowdb-adapter-aws-s3 )
154165
166+ - [ json-server] ( https://github.com/typicode/json-server )
167+ - [ serverless framework] ( https://serverless.com/ )
168+ - [ serverless http] ( https://github.com/dougmoscrop/serverless-http )
169+ - [ lowdb-adapter-aws-s3] ( https://github.com/nicekiwi/lowdb-adapter-aws-s3 )
155170
156171## Components
157- * [ NodeJS 8.10] ( https://nodejs.org/en/about/ )
158- * [ AWS API Gateway] ( https://aws.amazon.com/api-gateway/ )
159- * [ AWS Lambda] ( https://aws.amazon.com/lambda/features/ )
160- * [ AWS S3] ( https://aws.amazon.com/s3/ )
172+
173+ - [ NodeJS 8.10] ( https://nodejs.org/en/about/ )
174+ - [ AWS API Gateway] ( https://aws.amazon.com/api-gateway/ )
175+ - [ AWS Lambda] ( https://aws.amazon.com/lambda/features/ )
176+ - [ AWS S3] ( https://aws.amazon.com/s3/ )
161177
162178## Develop locally
179+
163180db.json file will be loaded directly from your local filesystem. No AWS access is needed.
164181
165182#### 1. Start solution
166- ```
183+
184+ ``` bash
167185npm run start
168186```
169187
170188#### 2. Test your API
171189
172190To test you can use e.g. [ Postman] ( https://www.getpostman.com/ )
173191
174-
175- * Open Postman
176- * Enter as Url the endpoints url
192+ - Open Postman
193+ - Enter as Url the endpoints url
177194
178195``` bash
179- https://localhost:3000/{route} # e.g. default value: https://localhost:3000/basic
196+ https://localhost:3000/{route} # e.g. default value: https://localhost:3000/posts/
180197```
181198
182-
183199What`s my {route} ? -> see [ json-server documentation] ( https://github.com/typicode/json-server )
184200
185-
186-
187201## Develop locally with cloud resources
202+
188203Use same componentes (S3, LowDB) as the lambda does but have code executed locally.
189204
190205#### 1. Add .env file to root folder
191206
192-
193207** 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 >
194208** Mind:** This function requires that you have access to AWS (e.g. via credentials)
195209
196- * Copy the .env file from .env.sample in the root folder
197- ```
210+ - Copy the .env file from .env.sample in the root folder
211+
212+ ``` bash
198213cp .env.sample .env
199214```
200215
201- * Required: Adapt settings in .env file
216+ - Required: Adapt settings in .env file
202217
203218| Attribute | Description | Type | Default |
204219| ---| ---| ---| ---|
@@ -216,26 +231,22 @@ npm run dev
216231
217232To test you can use e.g. [ Postman] ( https://www.getpostman.com/ )
218233
219-
220- * Open Postman
221- * Enter as Url the endpoints url
234+ - Open Postman
235+ - Enter as Url the endpoints url
222236
223237``` bash
224- https://localhost:3000/{route} # e.g. default value: https://localhost:3000/basic
238+ https://localhost:3000/{route} # e.g. default value: https://localhost:3000/posts
225239```
226240
227-
228241What`s my {route} ? -> see [ json-server documentation] ( https://github.com/typicode/json-server )
229242
243+ ## Diagnose issues
230244
231- ## Diagnose issues
232245serverless-offline will help you to troubleshoot issues with the lambda execution in a fast manner.
233246
234247** Mind:** The assumption is that the solution has been already deployed<br >
235248** Mind:** This function requires that you have access to AWS (e.g. via credentials)
236249
237-
238-
239250#### 1. build sources and execute serverless offline
240251
241252- sources will be build with babel in advance to test the functionality.
@@ -254,9 +265,9 @@ Serverless: Remember to use <b>x-api-key</b> on the request headers
254265#### 2. make api calls
255266
256267- Use a new terminal window and start to make api calls.
257- - Replace {API-KEY} with the api key in the sls offline output (see above).
258- - Replace {route} with the route you want to test e.g. /basic
268+ - Replace {API-KEY} with the api key in the sls offline output (see above).
269+ - Replace {route} with the route you want to test e.g. /posts
259270
260271<pre ><code >
261272curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" http://localhost:3000/{route}
262- </pre ></code >
273+ </pre ></code >
0 commit comments