go-api
An implementation of a simple RESTful API in Golang on AWS infrastructure.
Tech Stack
- Serverless framework
- Go language
- AWS API Gateway
- AWS Lambda
- AWS DynamoDB
Installation
Make sure you've got Golang
, aws-cli
, node.js
and serverless
installed.
For instructions regarding aws-cli
installation, check this link on AWS docs.
You may also find ways to install node.js
on this link. Serverless framework requires node.js
to work.
Assuming you've got node.js
and it's dependency manager, npm
installed; install Serverless framework using the command below. (Open up a Terminal if you use macOS or GNU/Linux, else try WIN + R
and type CMD
to open a command Prompt and then enter these commands!)
npm i -g serverless
This will install Serverless globally. Next step is to configure your aws-cli
.
Use below command to start the configuration process:
aws configure
Input the requested values accordingly. These are the private data regarding your AWS account.
Congratulations!
Scripts
I've provided some easy-to-use scripts for build
, test
, deploy
and clean
processes; as well as a Makefile for unix-like OS users. (Since GNU/make is not available on Windows)
Assuming you have a Terminal/Command Prompt open and located to the project root
path, change directory to scripts
where all the scripts are located.
cd scripts
-
Build
Windows:
.\build
Unix-like:
make build
Alternative command:
env GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o ../bin/ ../src/handlers/...
Will build the source code using
go build
forlinux
andamd64
architecture. -
Test
Windows:
.\test
Unix-like:
make test
Runs unit testing (using
*_test.go
files) and stores results intests
directory. Will open Go coverage report in the default web browser on Windows. -
Clean
Windows:
.\clean
Unix-like:
make clean
Alternative command:
rm -rf ../bin ../tests/*
Clears
bin
directory and test results located attests
directory. -
Deploy
Windows:
.\deploy
Unix-like:
make deploy
Alternative command:
cd .. && serverless deploy --verbose
Will handle deployment to AWS using Serverless framework.
Test Results
Handlers (Lambda functions):
-
src/addDevice/addDevice.go
PASSED with 95.1% coverage of statements.
-
src/getDeviceById/getDeviceById.go
PASSED with 91.5% coverage of statements.
- Tests do not cover inner AWS functions like
lambda.Start()
andsession.NewSession()
Demo
• Currently serving on dev
stage. You may change stage
name and region
in serverless.yml
file.
API Endpoints:
$ curl --location --request POST 'https://vssb3ixb3j.execute-api.us-west-1.amazonaws.com/dev/devices' --header 'Content-Type: application/json' --data-raw '{"id": "/devices/id1","deviceModel": "/devicemodels/id1","name": "Sensor","note": "Testing a sensor.","serial": "A020000102"}'
{"message":"Device successfully added to DynamoDB","statusCode":201}
$ curl --location --request GET 'https://vssb3ixb3j.execute-api.us-west-1.amazonaws.com/dev/devices/id1'
{
"data": {
"id": "id1",
"deviceModel": "/devicemodels/id1",
"name": "Sensor",
"note": "Testing a sensor.",
"serial": "A020000102"
}
}