Building a ToDo API with Golang and Kubernetes! – Part 1 – Introduction

Hi Folks,

Welcome to another tutorial series on building cloud native applications. In this series you will learn how to use

golang and MongoDB (mgo) to build a cloud native API microservice using golang and deploying it to a Kubernetes Cluster with your own Helm chart!


What will you learn?

  • How to import third party libraries in go
  • How to use mux to route your API endpoints
    • Create
    • Read
    • Update
    • Delete
  • How to use mgo to store data
    • Basic MongoDB Concepts
  • How to build your own Authentication Middleware with Mux and MongoDB
  • How to build a Docker container to run your API
    • Using bash to build your project
    • Creating a Dockerfile to package your project
  • How to Deploy a MongoDB cluster to Kubernetes using helm
  • Build a Helm Chart to Deploy your API
    • Define your Deployment
    • Define your Service
    • Define your Ingress

Lets get started!

Creating your project

In your $GOPATH create a new folder. Try to match the following.


this will prevent any conflicts with anyone elses project.

Now we have our project directory create a new file inside it called main.go with the following contents

package main

func main() {

This is the absolute bare minimum requirement for a go application. You can run this but you will have no output.
you can run it with

$ go run main.go

If you did run it you will have seen no output.

Importing mux and creating your first endpoint

What is mux?

Package gorilla/mux implements a request router and dispatcher for matching incoming requests to their respective handler.

The name mux stands for “HTTP request multiplexer”. Like the standard http.ServeMux, mux.Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions

awesome right? so lets import it.

First we need to get the package

$ go get -u

this will download it to $GOPATH/pkg making it available to you as a library.

Now in your main.go file under the package statement we add an import() with the library.

import ( 

thats us imported everything we need.

Handling our first endpoint

So now we have mux and its requirements installed lets define our first endpoint.

But before we do that we need to define our router. We will do this using the declare and initialise operator provided by go.

We declare this in the main function.

func main() {
     router := mux.NewRouter()

And thats the router declared. Its thats simple, I hope by now you realise how awesome go is!
Now we can declare the first endpoint. Our first endpoint will be a simple health check. So lets add it to the router, again in the main function. The router we declared provides a HandleFunc to create a handler. This takes a Path and Method parameter.

func main() {
     router := mux.NewRouter()
     router.HandleFunc("/health", Health).Methods("GET")

Now w\e have a handler we need to create our health check function.  To do this we will need to use the io library. Add it to your imports.
Our health check function will just return a 200 OK and a simple json string. So lets write the function above main().

func Health(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK) // Set 200 OK
    w.Header().Set("Content-Type", "application/json") 
    io.WriteString(w, `{"alive": true}`) // Send json to the ResponseWriter

its as easy as that, I have added comments for simplicity.
Now we have our endpoint we have one final thing to add to the main function to make it functional.

so lets use net/http to serve our routed content at the end of the main function.

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/health", Health).Methods("GET")
    log.Fatal(http.ListenAndServe(":8000", router))

And thats it. Our first endpoint is ready! Lets test it.
Run the main.go file with ‘go run main.go’ (there is no output it will look like its doing nothing)

In another terminal lets test our first endpoint with cURL.

$ curl http://localhost:8000/health
{"alive": true}

woohoo! Our endpoint works!EOF

Thats it for Part 1! Hope you enjoyed this guide and learned something new. Please check back for Part 2 where we will create our Create, Read and Update endpoints to Add and Read from MongoDB and return JSON ToDo Items!


Leave a Reply