Basically the application consists of a background task which fetches BTCUSD price from Binance.us every minute in MariaDB, and a RESTFUL API for queries of the followings from the database:
- Last Price;
- Price at a Time Point; and
- Average Price within a Time Range.
The application run on two docker containers:
- Golang App (docker image built based on golang:1.14.2-alpine);
- MariaDB (mariadb:10.5.1-bionic).
The two containers communicate through the docker network bgl.
Codes under the following folders focus on different functions:
Responsible for sending periodical requests to binance API to get the lastest BTCUSD prices, and storing the data into the database.
Responsible for communication with the Database. In this case, it is MariaDB.
Contains helper functions such as average value calculations, float64-string interconversions and timestamp formatting.
Restful API codes that accept http requests and respond to them.
- router.go: define routes of the API;
- controllers.go: accept HTTP Requests, determine which functions to call and repond with appropriate HTTP Reponses;
- responses.go: "bridges" between controllers.go and db.go.
-
Run
docker network create bgl
to docker network bgl. -
Run the following command to start the DB container service:
docker run --net=bgl --name test-server_mariadb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=bgl -e MYSQL_DATABASE=bgl -e MYSQL_USER=bgl -e MYSQL_PASSWORD=bgl -e MYSQL_DATABASE=bgl -d mariadb:10.5.1-bionic
-
Get the IP Address of DB container service with
docker inspect test-server_mariadb | grep IPAddress
-
In .env, set
DB_CONNECT=<IP Address>:3306
with the IP Address obtained in the previous step. -
In project root, run the following commands to start the application:
docker build -t test-server .
docker run -d --rm --name test-server --net=bgl -p 80:80 test-server
- In .env, set
DB_CONNECT=mariadb
. - In project root, run the following command to start the application:
docker-compose up -d
Make sure you have docker and docker-compose installed. Give about 30s - 1min for the system to initialize.
In project root, run the following commands to run the tests:
go get "github.com/stretchr/testify/assert"
go get "github.com/DATA-DOG/go-sqlmock"
go test ./db/... ./helpers/... ./server/...
eg. curl http://localhost/lastprice
Example result:
{"lastPrice":"8853.23","time":"2020-05-12 15:54:47 +0800 HKT"}
Endpoint: http://localhost/price/{timestamp}
eg. curl http://localhost/price/2020-05-13T15:23:00
Example result:
{"price":"8862.36","time":"2020-05-12 15:54:47 +0800 HKT"}
Endpoint: http://localost/averagePrice/{from}/{to}
eg. curl http://localhost/averagePrice/2020-05-13T14:23:00/2020-05-13T15:23:00
Example result:
{"avgPrice":"8830.28","timeRange":"2020-05-13T03:23:00 to 2020-05-13T04:23:00"}