Deploying Go applications

Overview

This document is a hands-on guide to deploying a simple Go web application in tsuru.

Creating the app

To create an app, you use the command app create:

$ tsuru app create <app-name> <app-platform>

For Go, the platform name is go! Let’s be over creative and develop a hello world tutorial-app, let’s call it “helloworld”:

$ tsuru app create helloworld go

To list all available platforms, use the command platform list.

You can see all your applications using the command app list:

$ tsuru app list
+-------------+-------------------------+--------------------------------+
| Application | Units State Summary     | Address                        |
+-------------+-------------------------+--------------------------------+
| helloworld  | 0 of 0 units in-service | helloworld.192.168.50.4.nip.io |
+-------------+-------------------------+--------------------------------+

Application code

A simple web application in Go main.go:

package main

import (
    "fmt"
    "net/http"
    "os"
)

func main() {
    http.HandleFunc("/", hello)
    fmt.Println("listening...")
    err := http.ListenAndServe(":" + os.Getenv("PORT"), nil)
    if err != nil {
        panic(err)
    }
}

func hello(res http.ResponseWriter, req *http.Request) {
    fmt.Fprintln(res, "hello, world!")
}

Deployment

You can just run tsuru app deploy command and your project will be deployed:

$ tsuru app deploy -a helloworld .
 tar: Removing leading `/' from member names
 /

 ---- Building application image ----
  ---> Sending image to repository (5.57MB)
  ---> Cleaning up

 ---- Starting 1 new unit ----
  ---> Started unit b21298a64e...

 ---- Binding and checking 1 new units ----
  ---> Bound and checked unit b21298a64e

 ---- Adding routes to 1 new units ----
  ---> Added route to unit b21298a64e

 OK

Now you can check your deployed app running tsuru app info

$ tsuru app info -a helloworld
Application: helloworld
Platform: go
Teams: admin
Address: helloworld.192.168.50.4.nip.io
Owner: admin@example.com
Team owner: admin
Deploys: 1
Pool: theonepool
Units: 1
+---------------------------------+---------+
| Unit                            | State   |
+---------------------------------+---------+
| helloworld-web-b21298a64e-8a64e | started |
+---------------------------------+---------+

App Plan:
+---------------+--------+-----------+---------+
| Name          | Memory | Cpu Share | Default |
+---------------+--------+-----------+---------+
| autogenerated | 0 MB   | 100       | false   |
+---------------+--------+-----------+---------+

Handling dependencies

If your app is split in packages, you should set the GO_PKG_PATH environment variable with the package name of your app:

$ tsuru env-set GO_PKG_PATH=github.com/tsuru/helloworld --app helloworld

If you have external dependencies, you should add them as vendored packages. An alternative solution is building your app locally and deploying it using the app deploy command. In this case, you would also need a Procfile:

$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o helloworld
$ echo "web: ./helloworld" > ./Procfile
$ tsuru app deploy --app helloworld ./helloworld ./Procfile

If your app has other files to include in the deploy command, like tsuru.yaml, include them as parameters in the above command as well.

Running the application

tsuru will compile and run the application automatically, but it’s possible to customize how tsuru compiles and runs the application. For more details, check the README of the Go platform: https://github.com/tsuru/basebuilder/blob/master/go/README.md.

Now that the app is deployed, you can access it from your browser, getting the IP or host listed in app list and opening it. For example, in the list below:

$ tsuru app list
+-------------+-------------------------+--------------------------------+
| Application | Units State Summary     | Address                        |
+-------------+-------------------------+--------------------------------+
| helloworld  | 1 of 1 units in-service | helloworld.192.168.50.4.nip.io |
+-------------+-------------------------+--------------------------------+

It’s done! Now we have a simple go project deployed on tsuru.

Now we can access your app in the URL displayed in app list (“helloworld.192.168.50.4.nip.io” in this case).

Going further

For more information, you can dig into tsuru docs, or read complete instructions of use for the tsuru command.