A simple Restful API on AWS using the following tech stack:
The API accepts the following JSON requests and produces the corresponding HTTP responses:
HTTP POST
URL: https://<api-gateway-url>/api/devices
Body (application/json):
{
"id": "/devices/id1",
"deviceModel": "/devicemodels/id1",
"name": "Sensor",
"note": "Testing a sensor.",
"serial": "A020000102"
}
HTTP 201 Created
HTTP 400 Bad Request
If any of the payload fields are missing. Response body should have a descriptive
error message for the client to be able to detect the problem.
HTTP 500 Internal Server Error
If any exceptional situation occurs on the server side.
HTTP GET
URL: https://<api-gateway-url>/api/devices/{id}
Example: GET https://api123.amazonaws.com/api/devices/id1
HTTP 200 OK
Body (application/json):
{
"id": "/devices/id1",
"deviceModel": "/devicemodels/id1",
"name": "Sensor",
"note": "Testing a sensor.",
"serial": "A020000102"
}
HTTP 404 Not Found
If the request id does not exist.
HTTP 500 Internal Server Error
If any exceptional situation occurs on the server side.
This project is small and simple. Therefore, I implemented Service architecture where a lambda function can handle different actions (responds to Http GET & Http POST). Micro-service architecture is another way where each lambda function is only responsible for one action. Please note that I separated data layer from logics of project.
After configuring the CLI to use the credentials of the IAM user by aws configure
command (You’ll also need to specify the default region to us-east-1
), do these steps:
-
Clone the project to
/src
directory that Go uses for its workspaces. Usecd simple-restful-api-aws
for navigating to project folder. -
To build, type the following command
env GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o bin/devices simple-restful-api-aws/device
- Make a zip file of the executable device file
zip -j bin/devices.zip bin/devices
- Deploy the project by serverless
sls deploy
To post a device use the following command. Make sure to change <rest-api-id>
. You can get it from the link shown after deploying.
curl -i -H "Content-Type: application/json" -X POST https://<rest-api-id>.execute-api.us-east-1.amazonaws.com/api/devices -d '{"id":"/devices/id1","deviceModel":"/devicemodels/id1","name":"Sensor","note":"Testing a sensor.","serial":"A020000102"}'
To get a device from database you can use this command:
curl -i https://<rest-api-id>.execute-api.us-east-1.amazonaws.com/api/devices/id1
I put tests in main_test.go
and dataLayer_test.go
. Total coverage of the statements is 97.7%. The coverage of main_test.go
and dataLayer_test.go
are 96.9% and 100%, respectively.
To see coverage of unit test go to /device
folder by cd device
and execute the following:
go test -coverprofile=cover.out
To view it in Html format:
go tool cover -html=cover.out