In this setup we will have five docker containers as follows.
- 1 Go container : This is the main container which will host our Go app.
- 1 Minio container : Object storage server with Amazon S3 compatible API.
- 1 Postgres container : Postgres DB server.
- 1 Redis container : Key-value store that functions as a data structure server.
- 1 Nginx container : This will be used as a reverse proxy server.
You have to have docker, docker composer and Go installed on your local machine.
Docker-composer installation guide
And also you have to add GOPATH to your local system path.
- You can confirm the GOPATH using
go env GOPATH
command. - Check this to learn how to add new path to your system path
On your local machine, clone this repository and build docker images issuing following commands.
go get github.com/monstar-lab/fr-circle-api
cd $(go env GOPATH)/src/github.com/monstar-lab/fr-circle-api
docker-compose build
It will take some time to complete these three commands as it has to pull all the images needed at first run.
docker-compose up
By Issuing above command, we can have our containers up and running in the foreground. Hence we can see all the logs in our terminal window.
(You will have to kill any applications which is using port 80.)
We can use CTRL + C
to stop containers and get the access back to our terminal.
docker-compose up -d
Using the flag -d, we can have our containers running in the background. However we cannot see logs in our terminal window.
In either mode we can now access our go application on http://localhost/
Issue the following command to stop and remove all running containers.
docker-compose down
When we start up our containers, our go application will start to serve and be watching for any file change.
Each time a go source file is changed, Our app will incrementally be rebuilt. So we don't need to worry about building our app again and again.
We can just refresh our browser to see the changes we make.
We can use any editor or IDE as our wish. However we must set up gometalinter and any editor related plugins that makes go coding fast and easy.
Some of the common editors are ....
- VScode
- GoLand
- Vim
- Emacs
- Atom
Since go container is linked to DB, Redis and Minio containers, We can connect to those service from our go source as follows.
- DBMS : postgres
- Host : db
- Port : 5432
- User : postgres
- Password : mypass
- DBname : sample
- Host : redis
- Port : 6379
- Endpoint : s3:9000
- Access key : AKIAIOSFODNN7EXAMPLE
- Secret Key : wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
If we want to import a new library to our code, we can issue a command as follows while containers are running. It will download the library to the project.
docker exec -it go-cont dep ensure -add github.com/foo/bar
(If you have go dep installed locally, you can do the same by dep ensure -add github.com/foo/bar
)
then we can import that library in our source code like import "github.com/foo/bar"
*** Do not use go get
in this environment to import libraries.
While the docker containers are up and running, use the following command to run all the go test files.
docker exec -it go-cont go test ./...
If docker containers are not running use the following command. (This will start up our containers by itself to run the test.)
docker-compose run go go test ./...
- Go container : go-cont
- Minio container : storage-cont
- Postgres container : db-cont
- Redis container : kvs-cont
- Nginx container : proxy-cont
Issue the Following command while the container is running.
Replace go-cont with any of the container name above.
docker exec -it go-cont sh
To analyze and download missing dependencies issue the following command while containers are running.
docker exec -it go-cont dep ensure
(If you have go dep installed locally, you can do the same by dep ensure
)
This file is responsible to create a new docker image by copying the Go source in go-app
directory, build and run it in the
container.
This file is useful for defining and running multi-container Docker applications like ours. We use this file to configure our application’s services. Then, by docker-compose build
command, we can create and start all the services.
These files are used by dep to track and manage dependencies for our project.
This is a basic configuration file grabbed from the stock nginx docker image. Server block is added to this file to get web requests (on ports 80,443) forwarded to the Go container.
This runs a http server that listens on port :8080
inside go container, so that we can access it via web browser on http://localhost:8080
. Since this is the starting point of our application, this file is responsible to route a url to the correct httphandler function.
we can access it via port http://localhost:80
as well since the proxy server is configured to forward all the requests to port 80 and 443 to the go container.
This file includes a sample code to understand how to deal with redis storage in this environment. Redigo is used in this sample code, which is a Go client for the Redis database.
This file includes a sample code to understand how to connect to postgres database in this environment.
This file is responsible for the followings.
http://localhost/db/
: lists all the records in a table called dummytable.http://localhost/db/add/{some text}
: inserts a new records to a table called dummytable with the value of {some text}.
This file includes a sample code to understand how to deal with minio object storage in this environment.
This file is responsible for the followings.
http://localhost/s3/
: lists all the objects in a bucket called testbucket.http://localhost/s3/triggeraput/
: uploads thes3_upload_test_file.txt
to the testbucket.
We can access the minio service from the minio server directly via http://localhost:9000
and check our objects.
Read official minio guide to learn more.
This file includes a simple test.
This test will run when we issue the docker-compose run go go test ./...
command which is mentioned in the test section above.