Setting up a Go webapp on App Engine

Google App Engine (GAE) is Google’s platform-as-a-service (PAAS) for cloud-hosted solutions, such as web applications.

An advantage of a PAAS is faster delivery of solutions– Google configures and maintains the infrastructure so a developer can focus more time on programming. No provisioning machine instances / configuring databases / ansible scripts for code deployments / setting up security groups / servlet container configuration… Just write code and push to the platform.

Contrasted with infrastructure-as-a-service (IAAS) offerings, such as AWS (Amazon’s Web Services), there’s a slight premium, and each situation has it’s own cost-benefit balance between time + money.

A great advantage of App Engine is it’s free tier, which is generous enough to power most low-traffic websites 247, and makes GAE a very attractive option for prototyping.

With a PAAS such as App Engine, there is the trade-off of “vendor lock-in”, which would be an issue if one wanted to switch hosting providers. While there are safeguards that could eliminate this, any PAAS includes a liklihood of being committed to that platform.

Golang in Google App Engine

If you haven’t already installed Go on your system, follow the instructions to install go before continuing on.

Download Google Cloud SDK for Go

To get set up on Google App Engine, download and install the Cloud SDK for Go.

It’s available for download here and includes instructions.

In summary, the steps on linux are:

Once done, you can go to any reopened Terminal and execute the command gcloud and see its output to verify setup.

Contrats! You’re configured locally to work with Google App Engine!

Set up a project

Direct your browser to the App Engine console and set up a new Project using the menu at the top, which will ask for a project name and geographic region New Project.

Our project name for this is m0d3rnc0ad.

The project’s *.appspot.com domain

Once complete, App Engine provisions a space for your project, publicly accessible at your-project-name.appspot.com, such as our project at:

http://m0d3rnc0ad.appspot.com/

Create a webapp

On your machine, choose where you want your webapp code to live. I created a new folder located at $GOWORK/src/bitbucket.org/theandruu/go-public/first-gae-go-app to hold the code.

Code files

Inside your chosen directory, make 2 files: app.yaml and hello.go with the contents:

app.yaml

runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

hello.go

package hello

import (
    "fmt"
    "net/http"
)

func init() {
    http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello cruel, cruel world!")
}

And that’s it! You have a working example of a simple webapp that can run on App Engine on your local machine.

Note about the Go code

Notice the file hello.go declares 'package hello'. We choose this (or any package other than ‘main’) since App Engine provides the ‘main’ package on the platform.

Run the webapp locally

From a Terminal located at the directory containing the code files above, run the server by executing:

dev_appserver.py app.yaml

And then point a browser to http://localhost:8080 and you can see your app running! New Project

You can shut down the local dev server by issuing a Control + C from the Terminal screen.

Deploy the webapp to App Engine

Within Terminal at the same directory, deploy your code to the App Engine project you created by executing:


# Sign in- will pop open a browser
gcloud auth login

# Set the project
gcloud config set project m0d3rnc0ad

# deploy from the directory containing app.yaml
gcloud app deploy app.yaml

# after deploy, to see the app running live (will open up a browser)
gcloud app browse

Accessing your project on App Engine

The version of your code just-deployed is immediately accessible at the URL http://your-project-name.appspot.com/

Within App Engine you can additionally set which version should be the ‘default’ version of your app accessible at your project’s root, migrate traffic, start/stop instances, etc: GAE Versions

Wrapup

We’ve covered what it takes to:

From here we’re all set up to dig deeper into App Engine and start building more impressive applications.

◀   Debugging Golang GAE/Golang quickstart with Bootstrap templates   ▶