Skip to content
A loaded GoLang app to do various DevOps POC's
Go Dockerfile Shell
Branch: master
Clone or download
iamabhishek-dubey Merge pull request #3 from opstree/development
[Development][Add] Added ELK monitoring
Latest commit 74c3098 Sep 21, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
Dockerfile [Development][Change] Refactored code Sep 17, 2019
Gopkg.lock [Master][Add] Added Opstree CRUD application for Golang Sep 12, 2019
Gopkg.toml [Master][Add] Added Opstree CRUD application for Golang Sep 12, 2019
LICENSE Initial commit Sep 12, 2019
docker-compose.yml [Development][Add] Added docker compose setup for application Sep 20, 2019
main.go [Master][Add] Added Opstree CRUD application for Golang Sep 12, 2019


The main goal of creating this sample Golang application is to provide an environment and idea of Golang Application's build, test and deploy phase.

The application looks likes this:-


  • Golang ---> For development environment
  • Docker ---> For dockerizing the application
  • Dep ---> For Golang dependency management


OT Go-App is a CRUD application which provides a Web UI Interface for Employee Management. As a functionality wise it provides:-

  • Web UI interface for employee management
  • It stores all the data in MySQL database
  • It provides functionality of auto-reconnection of database
  • Generates log file for access log and error in /var/log
    • Access logs will lies in /var/log/ot-go-webapp.access.log
    • Error logs will lies in /var/log/ot-go-webapp.error.log
  • We can pass the database credentials via properties file or environment variables
  • For properties file we have to store at this location /etc/conf.d/ot-go-webapp/application.ini and the content should be something like this :-
ENABLED     = true
DB_URL      =
DB_PORT     = 3306
DB_USER     = root
DB_PASSWORD = password

ENABLED    = true
  • For environment variables we have to set these environment variables

    • DB_USER ---> Name of the database user
    • DB_PASSWORD ---> Password of the database user
    • DB_URL ---> URL of the database server
    • DB_PORT ---> Port on which database is running
    • ENABLED ---> Enable redis for caching or not
    • REDIS_HOST ---> URL of the redis server
    • REDIS_PORT ---> PORT of the redis server
  • There is health check url also available at /health, which provides the information that application is healthy or not

Important:- In MySQL database should exist with name employeedb

Directory Layout

The folder structure of the codebase looks like this:-

├── Dockerfile        ---> Dockerfile to dockerize the complete application
├── Gopkg.lock        ---> Automated generated file by dep dependency manager
├── Gopkg.toml        ---> Automated generated file by dep dependency manager
├── main.go           ---> Main function file to call all the function
├── main_test.go      ---> Test case for main function calling
├──         ---> The file which you are going through right now
└── webapp            ---> Actual codebase for webapp folder
    ├── main.go       ---> Main file for calling webapps functions
    ├── main_test.go  ---> Main webapp test cases file
    ├── redis.go      ---> This file holds all the 
    ├── redis_test.go ---> Test cases for redis caching functionalities
    ├── sql.go        ---> This file holds all the sql functionalities related stuff
    ├── sql_test.go   ---> Test cases file for sql functionalities
    └── template.go   ---> This file has the HTML template for Web Interface

Monitoring Application

We have integrated ELK(Elasticsearch, Logstash, and Kibana) for application log monitoring example. The main motive for this to encourage people to use JSON logging because field separation is quite easy in JSON. If you hate writing grok patterns this feature could be your lifesaver.

In addition you will get these beautiful logs visualization

To run this setup follow these instructions:-

cd elk
export ELK_VERSION=7.3.1
docker-compose up -d

That's it this will lead you to this UI

Building Application

For non-dockerized environment

This webapp is written in go so it builds the application in the form of binary. For downloading the dependency through dep:-

dep ensure

If you are not using the dep use go for downloading the dependency but there is a fallback in this, you will not get the exact version 😜

go get -u ./...

After downloading the dependency only thing left is building it, for which we can use simple golang command line

go build

For dockerized environment

For dockerized environment just run:-

docker build -t opstreedevops/ot-go-webapp:latest -f Dockerfile .

Running Application

For non-dockerized environment

First we have to create a database in MySQL


Once the database is created, create a properties file or set environment as given above and then execute the binary


Now you will be able to access the app at http://<your_server_ip>:8080/ and health url at http://<your_server_ip>:8080/health

For dockerized environment

First we have to run the mysql and redis container

docker run -itd --name mysql -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=employeedb mysql:5.6
docker run -itd --name redis redis:latest

Once the mysql container is created, we have to start the application

docker run -itd --name application --link mysql:mysql -e DB_USER=root -e DB_PASSWORD=password -e DB_URL=mysql -e DB_PORT=3306 -e REDIS_HOST=redis -e REDIS_PORT=6379 opstreedevops/ot-go-webapp:latest

To Do

  • Implement logging
  • Add more fields for employee
  • Write unit tests
  • Fix code if there is any mess
  • Integrate dependency management
  • Fill README with more information
  • Make application more attractive
  • Add healthcheck API
  • Add redis healthcheck
  • Logging of acccess and error log
  • Provide file uploading functionality
  • Integrate redis for caching purpose
  • Dump manifests file for kubernetes deployment
  • Replace property file from ini structure file
  • Structure code in better manner(Refactoring)
  • Implement json logging
  • Add docker compose setup
  • Add ELK monitoring docker compose stack
You can’t perform that action at this time.