Howto Deploy a Go App on anynines PaaS & Cloud Foundry

Thanks to Cloud Foundry’s support of buildpacks it’s very easy to deploy almost any app of any programming language to the anynines PaaS. This, of course, includes the Go language.

In this blog post, we will create and deploy a sample go web app to the anynines PaaS and Cloud Foundry.

We assume, you’ve got Go 1.9.2 set up, an anynines user account and the anynines CLI tools.

Go Web App

We use the standard go libraries to implement a simple ‘Hello World’ http server.


main.go

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

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, World!")
}

func main() {
	port := os.Getenv("PORT")
	if len(port) < 1 {
		port = "8080"
	}

	http.HandleFunc("/", handler)
	fmt.Println("Listening on port", port)
	http.ListenAndServe(":"+port, nil)
}

The web server uses the port of the environment variable PORT provided by the application server (called Diego in Cloud Foundry Terms) when run on Cloud Foundry, otherwise it listen to port 8080.

Execute the sample app on your machine:


$ go run main.go

The following message will appear:


Listening to port 8080

Open https://localhost:8080 in your browser.

Deployment

Let’s deploy your app to anynines.
First, you have to login to anynines with your credentials:


cf login -a https://api.de.a9s.eu

You will be asked for a space. You can use ‘test’ for testing purposes.

Now a manifest file is needed to tell anynines what to do with the web app.


manifest.yml

---
applications:
- name: go-to-web
  instances: 1
  memory: 64MB
  host: go-to-web
  buildpack: https://github.com/cloudfoundry/go-buildpack.git
  env:
    GOPACKAGENAME : go-to-web
  • The manifest may begin with three dashes.
  • The applications block begins with a heading followed by a colon.
name

is the application name.

instances

and

memory

specify the number of instances and how much memory the application can consume.

host

provides a hostname or subdomain in the form of a string. The host is needed to address your application. Ensure the name is unique, otherwise it is possible the host is already taken and you’ll get an error upon deployment.

buildpack

specifies the custom buildpack to deploy the application. The buildpack provides framework and runtime support for our go app.

  • Set the environment variable
    GOPACKAGENAME

    to your app’s name. The buildpack will then be able to detect and compile your go app. Additionally, you can use native go vendoring in the future.

Now there must be two files in the folder.


main.go
manifest.yml

The command to push the app is quite simple:


$ cf push

When successful, the result can be found at https://<your_host>.de.a9sapp.eu

You can check the status of your app with:

$ cf app <your_app_name>

Showing health and status for app go-to-web
OK

requested state: started
instances: 1/1
usage: 64M x 1 instances
Urls: go-to-web.de.a9sapp.eu
package uploaded: Tue Dec 7 06:04:46 UTC 2017

     state     since                    cpu    memory      disk   
#0   running   2017-12-07 08:05:38 AM   0.0%   4M of 64M   7.4M of 256M   

Find a working example of the anynines go example app on github & have fun.

Leave a Reply

Your email address will not be published. Required fields are marked *