Learn to build and deploy simple Go Web Apps, Part One

In this 4-part series of tutorials, I’m going to take you through the process of creating a fully functioning Go web app called GoView, using just pure Go and deploy to Google App Engine. Hopefully, this will help to demonstrate just how quick and easy it is to use Go for web apps. Let’s begin with the basics.

Assumption

I am assuming that you have downloaded and installed Go and have set the GOPATH properly. If new to Go, then my previous article has this covered.

Google App Engine

Google App Engine is different from most other cloud systems because it is neither IaaS (Infrastructure-as-a-Service, e.g., Amazon EC2) nor SaaS (Software-as-a-Service, e.g., Salesforce). It is something in-between – PaaS (Platform-as-a-Service). Instead of a fixed application (SaaS) or raw hardware (IaaS), App Engine manages your infrastructure for users. Furthermore, it provides a development platform… users get to create apps, not use the ones provided by the cloud vendor, and it leverages the infrastructure as a hosting platform.

Google App Engine lets you run web applications on Google’s infrastructure. With App Engine, there are no servers to maintain: You just upload your application, and it is ready to serve your users. App Engine costs nothing to get started. All applications can use up to 1 GB of storage and enough CPU and bandwidth to support an efficient app serving around 5 million page views a month, absolutely free.

Creating an App Engine application is easy, and only takes a few minutes. And it is free to start: upload your app and share it with users right away, at no charge and with no commitment required.

Sandbox

Developers would not be interested in letting other applications/users get any kind of access to their application code or data. To ensure this, all App Engine applications run in a restricted environment known as a sandbox.

This is a Warning

Because of the sandbox, applications can’t execute certain actions. These include: open a local file for writing, open a socket connection, and make operating system calls.

Services

The App Engine team has created a set of higher-level APIs/services for developers to use. Want your app to send and receive e-mail or instant messages? That’s what the e-mail and XMPP APIs are for! Want to reach out to other web applications? Use the URLfetch service! Need Memcache? Google has a global Memcache API. Need a database? Google provides both its traditional NoSQL scalable datastore and access to the relational MySQL-compatible Google Cloud SQL service.

The list of all the services that are available to users changes quite often as new APIs are created.

The Administration Console

The Google App Engine Administration Console gives you complete access to the public version of your application. Access the Console by visiting this link in your web browser.

Applications (web and non-web)

While many applications running on Google App Engine are web-based apps, they are certainly not limited to those. App Engine is also a popular backend system for mobile apps. When developing such apps, it’s much safer to store data in a distributed manner and not solely on devices which could get lost, stolen, or destroyed. Putting data in the cloud improves the user experience because recovery is simplified and users have more access to their data.

DataStore

App Engine Datastore is a schemaless object datastore providing robust, scalable storage for your web application. This means that you can’t run MongoDB, for example, on the Google App Engine (GAE). You need to use the Google datastore. However, GAE now supports Google Cloud SQL a fully managed MySQL service hosted on Google Cloud Platform.

The Go runtime environment

With the Google App Engine for Go, you can build web applications using the Go Programming Language. Your Go application runs on Google’s scalable infrastructure and uses large-scale persistent storage and services.

App Engine builds and executes Go application code using a safe “sandboxed” environment. Your app receives web requests, performs work, and sends responses by interacting with this environment.

The Go runtime environment uses the latest version of Go version 1. The SDK includes the Go compiler and standard library, so it has no additional dependencies. As with the other runtimes, not all the standard library’s functionality is available inside the sandbox. For example, attempts to open a socket or write to a file will return an os.ErrPermission error. Instead, apps use scalable services provided by App Engine to store data and communicate over the Internet.

The SDK includes an automated build service to compile your app, so you’ll never need to invoke the compiler yourself. And your app will be automatically re-built whenever you change the source.

Download and Install the App Engine SDK

To start developing Google App Engine applications in Go, you first download and set up the App Engine Go software development kit (SDK).

The Go SDK includes a web server application that simulates the App Engine environment, including a local version of the datastore, Google Accounts, and the ability to fetch URLs and send email directly from your computer using the App Engine APIs.

The Go SDK will run on any Intel-based Mac OS X, Linux or Windows computer with Python 2.7. If necessary, download and install Python 2.7 for your platform from the Python web site. Most Mac OS X users already have Python 2.7 installed. If you have issues with the Python tools, please ensure you have Python 2.7 installed.

Let us now download the App Engine SDK. Next follow the instructions on the download page to install the SDK on your computer.

I installed the App Engine SDK to C:\go_appengine on my Windows 7 desktop.

Later on in this article, we will use the following two commands from the SDK:

You can find these commands in the C:\go_appengine directory. To simplify development and deployment, consider adding this directory to your PATH environment variable.

Tip:

While setting the PATH ensure that C:\go_appengine comes after C:\go\bin i.e. it should be like PATH=C:\go\bin;C:\go_appengine;... This ensures that we use the go command from the original Go installation and not the go command from the App Engine.

Let us build a trivial web app (welcome.go) locally

The local development environment lets you develop and test complete App Engine applications before showing them to the world. Let us write some code.

Go App Engine applications communicate with the outside world via a web server compatible with Go’s http package. This makes writing Go App Engine applications very similar to writing stand-alone Go web applications.

Let us begin by implementing a tiny application that displays a short message to a user.

Program welcome.go

Inside the folder $GOPATH/src/github.com/SatishTalim create the folder welcome.

Next inside the welcome folder, create a file named welcome.go, and give it the following contents:

package welcome

import (
        "fmt"
        "net/http"
)

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

func handler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Welcome from the language of the coming decade - Go!")
}

This Go package responds to any request by sending a response containing the message:
Welcome from the language of the coming decade - Go!

Note:

  • when writing a stand-alone Go program we would place this code in package main. The Go App Engine Runtime provides a special main package, so you should put HTTP handler code in a package of your choice (in this case, welcome).
  • to work with some printing functions, we import the package fmt.
  • the App Engine Go API uses the standard http package as an interface between your Go program and the App Engine servers. Thus for web related http functionality, we import the package http. Any functions within that we refer as http.function_name.
  • within the init program, we redirect any incoming requests to the handler function. We do this by calling http.HandleFunc and passing it two parameters – the first one is a part of the incoming url, and the second is the method capable of handling it.
  • the function handler takes an http.ResponseWriter and an http.Request as its arguments.
  • when a user connects, the programs responds with a text that is sent back to the browser. The http.ResponseWriter value assembles the HTTP server’s response; by writing to it, we send data to the HTTP client.
  • an http.Request is a data structure that represents the client HTTP request.
  • all the parameters of a request can be received via the parameter http.Request in the handler. You can get the URL, the input values and other details.

Create the Configuration File

An App Engine application has a configuration file called app.yaml. Among other things, this file tells the App Engine service which runtime to use and which URLs should be handled by our Go program.

Inside the $GOPATH/src/github.com/SatishTalim/welcome directory, create a file named app.yaml with the following contents:

# This is a comment

# application is mandatory
# later on the web, we might use some other name
application: welcome

# version is mandatory
version: 1-0

# runtime is mandatory
runtime: go

# api_version is mandatory
api_version: go1

# handlers is mandatory
handlers:
- url: /.*
  script: _go_app

From top to bottom, this configuration file says the following about this application:

  • The application identifier is welcome. When you register your application with App Engine later on, you will select a unique identifier, and update this value. This value can be anything during development. For now, leave it set to welcome.
  • This is version number 1-0 of this application’s code. Your application versioning information can contain alphanumeric characters, and hyphens. If you adjust this before uploading new versions of your application software, App Engine will retain previous versions, and let you roll back to a previous version using the administrative console.
  • This code runs in the go runtime environment, with API version go1.
  • There are two kinds of handlers: script handlers, and static file handlers. A script handler runs a Python script in your application to determine the response for the given URL. A static file handler returns the contents of a file, such as an image, as the response.
  • Static files are files to be served directly to the user for a given URL, such as images, CSS stylesheets, or JavaScript source files. Static file handlers describe which files in the application directory are static files, and which URLs serve them.
  • url is a URL prefix. This value uses regular expression syntax (and so regexp special characters must be escaped \). All URLs that begin with this prefix are handled by this handler, using the portion of the URL after the prefix as part of the file path.
  • Every request to a URL whose path matches the regular expression /.* (all URLs) should be handled by the Go program.
  • For Go apps, script should always have a value of _go_app.

Note: All Go packages for a given app are built into a single executable, and request dispatch is handled by the Go program itself. This is why we call http.HandleFunc inside the init function to associate our handler with the web root ("/"). However, you may still use the app.yaml file to configure paths that serve static files or require special permissions.

For a complete list of configuration options, see the Go Application Configuration page.

Test the App

You can now test your app with the web server included with the App Engine SDK.

The application’s directory should contain the files welcome.go and app.yaml.

From the $GOPATH/src/github.com/SatishTalim directory run the following command, to compile your app and start the development web server:

$ goapp serve welcome/

The web server is now running, listening for requests on port 8080. Test the application by visiting the following URL in your web browser: http://localhost:8080/. For more information about running the development web server, including how to change which port it uses, see the Development Server reference.

Iterative Development

The development app server knows to watch for changes in your file. As you update your source, it re-compiles them and relaunches your local app. There’s no need to restart goapp serve.

Try it now: leave the web server running, then edit welcome.go to change Welcome from the language of the coming decade - Go! to something else. Reload http://localhost:8080/ to see the change.

To shut down the web server, make sure the terminal window is active, then press Control-C (or the appropriate “break” key for your console).

Eureka! You now have a complete working App Engine web application!

In part two, you will learn to deploy this simple web app to Google App Engine and share it with users worldwide.

I hope you’ve enjoyed this post, please leave any feedback in the comments section.

You can find the other parts of the series at the following links:

About these ads

About indianguru

http://satishtalim.com/
This entry was posted in Tutorials, Go and tagged , , . Bookmark the permalink.

6 Responses to Learn to build and deploy simple Go Web Apps, Part One

  1. I have build these and its running successfully but while starting goapp engine its prints message Could not initialize images API; you are likely missing the Python “PIL” module

  2. Pingback: Learn to build and deploy simple Go Web Apps, Part Two | Josh Software – Where Programming is an Art!

  3. Viola Allen says:

    Nice article especially for the beginners. I would still put emphasis on Amazon and Google as these are the best IaaS.

  4. indianguru says:

    Thanks Viola. Yes, the article is meant for Golang newbies. In part 4, we shall upload a new app “GoView” to the Google App Engine. I feel Heroku is better. Do check out this article – http://james-eady.com/blog/2013/08/06/hosted-golang-heroku-vs-google-app-engine/

  5. Pingback: Learn to build and deploy simple Go Web Apps, Part Three | Josh Software – Where Programming is an Art!

  6. Pingback: Learn to build and deploy simple Go Web Apps, Part Four | Josh Software – Where Programming is an Art!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s