Creating a Stateless RESTful Go App

Getting Started

Before starting this lab, you will need to have completed:

  1. The Previous Go lab for the Docker registry and Load Balancer setup - this demo will just update the previous Docker image with new functionality

Overview

In this lab, we will be enhancing the work done in the previous lab to introduce a RESTful API and walking through the steps necessary to build and deploy this to Critical Stack.

The Go code was lifted from Making a RESTful JSON API in Go and adapted to Critical Stack. This is purely sample code and not intended to be used as a reference for best practices.

Building Your RESTful Hello World App

  1. Clone the accompanying Critical Stack Feature Lab repo if you haven’t already and change working directories to the hello_rest directory so you can access the source code:

    git clone https://github.com/criticalstack/labs-code.git
    cd labs-code/go/hello_rest
  2. As with previous lab, compile the REST API example code into hello-go.

    $ CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -a -o hello-go .
  3. As with previous lab, build a Docker image using the Dockerfile.

    docker build -t hello-go -f Dockerfile .

Testing your RESTful Hello World App

Optional Step: If you want to test your new docker container locally and see if it behaves as expected you can follow these 6 steps:

  1. Run the following command:

    docker run -e PORT=8080 -p 8080:8080 --rm -ti hello-go
  2. Verify the app works by using the following command in a new shell window. Alternatively, you could open a browser to http://localhost:8080

Basic test:

$ curl -s http://localhost:8080
Welcome!
  1. Show entries pre-inserted:

    $ curl -s http://localhost:8080/todos | python -m json.tool
    [
    	{
    		"completed": false,
    		"due": "0001-01-01T00:00:00Z",
    		"id": 1,
    		"name": "Write presentation"
    	},
    	{
    		"completed": false,
    		"due": "0001-01-01T00:00:00Z",
    		"id": 2,
    		"name": "Host meetup"
    	}
    ]
  2. Show entry 1:  

    curl -s http://localhost:8080/todos/1 | python -m json.tool
    {
    	"completed": false,
    	"due": "0001-01-01T00:00:00Z",
    	"id": 1,
    	"name": "Write presentation"
    }	
  3. Insert a new entry:

    $ curl -s -H "Content-Type: application/json" -d '{"name":"New Todo"}' http://localhost:8080/todos
    {"id":3,"name":"New Todo","completed":false,"due":"0001-01-01T00:00:00Z"}	
  4. Show entry 3:

    $ curl -s http://localhost:8080/todos/3 | python -m json.tool
    {
    	"completed": false,
    	"due": "0001-01-01T00:00:00Z",
    	"id": 3,
    	"name": "New Todo"
    }

To stop the running container you can execute the following command: $docker stop $(docker ps -qf “ancestor=hello-go”)

Tagging and Pushing to a Container Registry

  1. Tag the new image with tag 0.0.2 using your image/repo/tag

    docker tag hello-go jabbottc1/hello-go:0.0.2
  2. Push to a docker registry

    $ docker push jabbottc1/hello-go:0.0.2
    The push refers to repository [docker.io/jabbottc1/hello-go]
  3. Edit your hello-go-deployment from the Critical Stack UI. Update the image in the Deployment to pull 0.0.2.

    image: 'jabbottc1/hello-go:0.0.2'  # Change this tag
  4. Save your deployment and Exit

  5. Test your upgraded deployment. You can run the same tests as above but targeting the Load Balancer.

    $ curl -s https://<URL_to_your_application>/todos | python -m json.tool
    [
    	{
    		"completed": false,
    		"due": "0001-01-01T00:00:00Z",
    		"id": 1,
    		"name": "Write presentation"
    	},
    	{
    		"completed": false,
    		"due": "0001-01-01T00:00:00Z",
    		"id": 2,
    		"name": "Host meetup"
    	},
    	{
    		"completed": false,
    		"due": "0001-01-01T00:00:00Z",
    		"id": 3,
    		"name": "John Test"
    	}
    ]

Conclusion

You have successfully deployed a RESTful Go application to Critical Stack. But this application can’t scale beyond 1 node and has no persistence! So next we will update the application with persistence and scale the deployment to multiple instances.