Table of Contents
- Preface
-
1. Deploying Go Web Apps to Heroku
- 1.1 Cloud Computing Service Levels
- 1.2 Create an account on Heroku
- 1.3 Install the Heroku Toolbelt
- 1.4 Prepare a web app
- 1.5 Use Git
- 1.6 Create a Procfile
- 1.7 Install Godep
- 1.8 Declare app dependencies
- 1.9 Using godep with our project
- 1.10 Add these new files to git
- 1.11 Deploy the app
- 1.12 Program gomongohq.go
Preface
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
Who is the eBook for?
This short eBook will introduce you to building your own web applications and hosting it on the Internet. It is targetted towards Go programming newbies who have:
- an understanding of basic web technologies HTTP,
- an understanding of HTML,
- a working knowledge of Go programming (to try out the programs in this eBook, you should have a working copy of Go 1.5 on your computer),
- an understanding of JSON.
Acknowledgements
There are a good number of people who deserve thanks for their help and support they provided, either while or before this eBook was written, and there are still others whose help will come after the eBook is released. I would like to thank my “gang” of mentors at RubyLearning, Doug Sparling and Sanat Gersappa for their help in making this eBook far better than I could have done alone.
How to Use This eBook
I recommend that you go through the entire eBook chapter by chapter, reading the text and running the sample programs. There are no large applications in this eBook – just small, self-contained sample programs. This will give you a much broader understanding of how things are done (and of how you can get things done), and it will reduce the chance of anxiety, confusion, and worse yet, mistakes.
Using Code Examples
All of the code in this eBook can be used pretty much anywhere and anyhow you please.
Getting the Code
You can get a .zip
or .tar
archive of the code by going to GitHub Repo and clicking on the “Download” button.
How to Contact Me
I can be reached via e-mail at satish@rubylearning.org. Please contact me if you have any questions, comments, kudos or criticism on the eBook. Constructive criticism is definitely appreciated; I want this eBook to get better through your feedback.
Thanks
Thanks for buying and checking out this eBook. As part of the lean publishing philosophy, you’ll be able to interact with me as the eBook is completed. I’ll be able to change things, reorganize parts, and generally make a better eBook. I hope you enjoy.
1. Deploying Go Web Apps to Heroku
There are plenty of definitions for “cloud computing” online, and for the most part, they generally point to the same thing: taking applications and running them on infrastructure other than your own. Companies or individuals who offload or effectively “outsource” their hardware and/or applications are running those apps “in the cloud.”
1.1 Cloud Computing Service Levels
In the figure below, you can see how the analyst firm Gartner segregates cloud computing into three distinct classes of service.
1.1.1 SaaS
Let’s start at the highest level: software applications that are only available online fall into the “Software-as-a-Service” category, also known as “SaaS”. The simplest example to understand is e-mail. For personal e-mail, people typically select from a variety of free web-based e-mail servers such as Google’s Gmail, Yahoo!Mail, or Microsoft’s Hotmail, rather than setting up all of the above through their provider. Not only is it “free” (supported through advertising), but users are freed from any additional server maintenance. Because these applications run (and store their data online), users no longer need to worry about managing, saving, and backing up their files. Of course, now it becomes Google’s responsibility to ensure that your data is safe and secure. Other examples of SaaS include Salesforce, IBM’s NetSuite, and online games.
1.1.2 IaaS
On the opposite end of the spectrum, we have “Infrastructure-as-a-Service,” or “IaaS,” where you outsource the hardware. In such cases, it’s not just the computing power that you rent; it also includes power, cooling, and networking. Furthermore, it’s more than likely that you’ll need storage as well. Generally IaaS is this combination of compute and cloud storage.
When you choose to run your applications at this cloud service level, you’re responsible for everything on the stack that is required to operate above it. By this, we mean necessities such as the operating system followed by additional (yet optional services) like database servers, web servers, load-balancing, monitoring, reporting, logging, middleware, etc. Furthermore, you’re responsible for all hardware and software upgrades, patches, security fixes, and licensing, any of which can affect your application’s software stack in a major way.
1.1.3 PaaS
In the middle, we have “Platform-as-a-Service,” or “PaaS.” At this service level, the vendor takes care of the underlying infrastructure for you, giving you only a platform with which to (build and) host your application(s). Gone are the hardware concerns of IaaS, yet with PaaS, you control the application — it’s your code — unlike as the SaaS level where you’re dependent on the cloud software vendor. The only thing you have to worry about is your application itself.
Systems like Google App Engine, Salesforce’s Heroku and force.com, Microsoft Azure, and VMwares Cloud Foundry, all fall under the PaaS umbrella.
A number of Platform-as-a-Service (PaaS) providers allow you to use Go applications on their clouds.
Heroku is a new approach to deploying web applications. Forget about servers; the fundamental unit is the app. Develop locally on your machine just like you always do. When you’re ready to deploy, use the Heroku client gem to create your application in their cloud, then deploy with a single git push. Heroku has full support for Go applications.
We shall soon see how we can deploy an app to Heroku.
1.2 Create an account on Heroku
Please ensure that you are connected to the internet and then create an account on Heroku (obviously do this only once). If you don’t have one, then signup. It’s free and instant. A free account can have up to 5 apps without registering your credit card.
1.3 Install the Heroku Toolbelt
The Heroku Toolbelt provides you access to the Heroku Command Line Interface (CLI). Once installed, you’ll have access to the heroku
command from your command window. Log in using the email address and password you used when creating your Heroku account:
1.4 Prepare a web app
In this step, you will prepare a simple Go application that can be deployed.
Create a folder webapphr
under the folder $GOPATH/src/github.com/SatishTalim/
and write the program webapphr.go
in the folder webapphr
as follows:
1.5 Use Git
In order to deploy to Heroku we’ll need the app stored in Git. In the same folder i.e. $GOPATH/src/github.com/SatishTalim/webapphr
type:
1.6 Create a Procfile
Use a Procfile
, a text file in the root directory of your application ($GOPATH/src/github.com/SatishTalim/webapphr
), to explicitly declare what command should be executed to start your app.
The Procfile
looks like this:
web: webapphr
This declares a single process type, web
, and the command needed to run it. The name web
is important here. It declares that this process type will be attached to the HTTP routing stack of Heroku, and receive web traffic when deployed.
1.7 Install Godep
The recommended way to manage Go package dependencies on Heroku is with Godep, which helps build applications reproducibly by fixing their dependencies.
Let us install Godep
:
$ go get github.com/tools/godep
1.8 Declare app dependencies
Heroku recognizes an app as a Go app by the existence of a Godeps.json
file in the Godeps
directory located in your application’s root directory ($GOPATH/src/github.com/SatishTalim/webapphr
).
The Godeps/Godeps.json
file is used by Godep
and specifies both the dependencies that are vendored with your application and the version of Go that should be used to compile the application.
When an app is deployed, Heroku reads this file, installs the appropriate Go version and compiles your code using godep go install ./…
.
1.9 Using godep with our project
In the folder $GOPATH/src/github.com/SatishTalim/webapphr
type:
$ godep save -r
This will save a list of dependencies to the file Godeps/Godeps.json
.
Note: Read the contents of Godeps/_workspace
and make sure it looks reasonable. Godep
does not copy files from source repositories that are not tracked in version control. Then commit the whole Godeps
directory to version control, including Godeps/_workspace
.
1.10 Add these new files to git
Now we’re ready to ship this to Heroku.
1.11 Deploy the app
In this step, you will deploy the app to Heroku.
Create an app on Heroku, which prepares Heroku to receive your source code.
When you create an app, a git remote (called heroku
) is also created and associated with your local git repository.
Heroku generates a random name (in this case thawing-harbor-9085
) for your app, or you can pass a parameter to specify your own app name.
Now deploy your code:
The application is now deployed.
Visit the app at the URL generated by its app name. As a handy shortcut, you can open the website as follows:
$ heroku open
That’s it — you now have a running Go app on Heroku!
1.12 Program gomongohq.go
Previously we had written the program mongohqconnect.go
. We shall modify that program and store the modified version in the file gomongohql.go
. We shall host gomongohql.go
on Heroku; connect it to our database godata
hosted on MongoLab and fetch information (the email id of say user Stefan Klaste) from the collection user
.
The program by now should be self-explanatory.
- The
rootForm
uses Pure – a set of small, responsive CSS modules that you can use in every web project. - The function
display
uses thehtml/template
package and themgo
driver to access the database on MongoLab. If the name is found in the database the function throws a page to the user with the email id for that name.
Note: Before you deploy gomongohql.go
to Heroku. Remember to:
- Read the contents of
Godeps/_workspace
. You will observe that you need to add the foldersrc/labix.org
with all its contents underGodeps/_workspace
. Then commit the wholeGodeps
directory to version control, includingGodeps/_workspace
. - After you have deployed the app to Heroku, remember to set the
heroku config:set MONGOLAB_URL=mongodb://IndianGuru:dbpassword@ds051523.mongolab.com:51523/godata
and thenheroku open
.