Run Revel Apps on Heroku
UPDATE: There have been some updates to my Revel Heroku Buildpack that make it work better and with newer versions of Revel. Check out the details.
Revel is a Play-like web framework for Go. I’m new to the Go programming language but I’ve heard good things. So I thought I’d take Revel for a spin and get it working on Heroku. Luckily there is already a Go Buildpack and a great article on how to use it.
To get Revel working on Heroku I had to make a few changes to Revel and create a modified buildpack. But it all works! Lets walk through the steps you can follow to deploy Revel apps on Heroku.
First lets get a simple Revel app working locally.
Step 1) Install Go
Step 2) Install Mercurial
Step 3) Install Git
Step 4) Create a new directory that will contain everything:
mkdir ~/go |
Step 5) This new “go” directory will be your “GOPATH” so set that environment variable:
export GOPATH=~/go |
Step 6) In the newly created “go” directory, get the original Revel source and my changes:
go get github.com/robfig/revel go get github.com/jamesward/revel |
Step 7) Build the “revel” command:
go build -o bin/revel github.com/jamesward/revel/cmd |
Step 8) Grab my “hellorevel” sample app:
go get github.com/jamesward/hellorevel |
Step 9) Start the local Revel server:
bin/revel run github.com/jamesward/hellorevel |
Step 10) In your browser navigate to: http://localhost:9000
You should see a “hello, Revel” message. Lets walk through this simple app so you can get an idea of how it works.
The request you just made is mapped to code through the “conf/routes” file which contains:
# Routes # This file defines all application routes (Higher priority routes first) # ~~~~ GET / Application.Index |
This tells Revel to handle HTTP GET requests to “/” with the “Application.Index” controller.
The “Application.Index” function is defined in the “app/controllers/app.go” file and contains:
package controllers import ( "github.com/robfig/revel" ) type Application struct { *rev.Controller } func (c Application) Index() rev.Result { message := "hello, Revel" return c.Render(message) } |
The “Index()” function creates a “message” string of “hello, Revel” and then returns the rendered template which received the “message” as a parameter.
The “app/views/Application/Index.html” template is used to get the HTML for the “Application.Index()” controller and contains:
<!DOCTYPE html> <html> <head> <title>{{.message}}</title> </head> <body> <h1>{{.message}}</h1> </body> </html> |
This uses Go templates and uses the “message” parameter.
Feel free to make changes to the template and controller to see how Revel auto-recompiles the source.
Ok, now that you have the application running locally, lets deploy it on Heroku.
Step 1) Signup for a free Heroku account
Step 2) Install the Heroku Toolbelt
Step 3) Login to Heroku from the command line (this should also setup your SSH keys if you haven’t done so already):
heroku login |
Step 4) Enter the directory for the “hellorevel” app and create a new application on Heroku which will use the Revel Buildpack:
cd ~/go/src/github.com/jamesward/hellorevel heroku create --buildpack https://github.com/jamesward/heroku-buildpack-go-revel.git |
Step 5) Upload the app to Heroku using Git:
git push heroku master |
This will upload the application source, pull in all of the dependencies, and then deploy the application on Heroku. That process will look something like this:
jamesw@T420s:~/go/src/github.com/jamesward/hellorevel$ git push heroku master Counting objects: 25, done. Delta compression using up to 4 threads. Compressing objects: 100% (19/19), done. Writing objects: 100% (25/25), 2.46 KiB, done. Total 25 (delta 2), reused 0 (delta 0) -----> Heroku receiving push -----> Fetching custom git buildpack... done -----> Revel app detected -----> Installing Go 1.0.3... done Installing Virtualenv...running virtualenv done Installing Mercurial... done -----> Copying src to .go/src/pkg/_app -----> Getting and building Revel -----> Discovering process types Procfile declares types -> (none) Default types for Revel -> web -----> Compiled slug size: 31.0MB -----> Launching... done, v4 http://polar-cove-5542.herokuapp.com deployed to Heroku To git@heroku.com:polar-cove-5542.git * [new branch] master -> master |
Step 6) Check out your Revel app on the cloud by running:
heroku open |
That’s it! Deployment of Revel apps couldn’t be easier! Let me know if you have any questions or feedback on this. Thanks!
About James Ward
James Ward (www.jamesward.com) works for Typesafe where he teaches developers the Typesafe Stack (Play Framework, Scala, and Akka) . James frequently presents at conferences around the world such as JavaOne, Devoxx, and many other Java get-togethers. Along with Bruce Eckel, James co-authored First Steps in Flex. He has also published numerous screencasts, blogs, and technical articles. Starting with Pascal and Assembly in the 80′s, James found his passion for writing code. Beginning in the 90′s he began doing web development with HTML, Perl/CGI, then Java. After building a Flex and Java based customer service portal in 2004 for Pillar Data Systems he became a Technical Evangelist for Flex at Adobe. In 2011 James became a Principal Developer Evangelist at Salesforce.com where he taught developers how to deploy apps on the cloud with Heroku. James Tweets as @_JamesWard and posts code at github.com/jamesward.
More About James »Northern Virginia Software Symposium
November 1 - 3, 2013
Reston, VA
Current Topics on the NFJS Tour
- Core Java, JEE
- Dynamic Languages: Groovy, JRuby, Scala, Clojure
- RESTful Web Apps
- Frameworks: Hibernate, Grails, Spring, JSF, GWT, more
- Agility
- Test Driven Design
- Security
- Ajax, Flex, RIA
Why Attend the NFJS Tour?
- » Cutting-Edge Technologies
- » Agile Practices
- » Peer Exchange
Current Topics:
- Languages on the JVM: Scala, Groovy, Clojure
- Enterprise Java
- Core Java, Java 7
- Agility
- Testing: Geb, Spock, Easyb
- REST
- NoSQL: MongoDB, Cassandra
- Hadoop
- Spring 3
- Automation Tools: Git, Hudson, Sonar
- HTML5, Ajax, jQuery, Usability
- Mobile Applications - iPhone and Android
- More...
NFJS, the Magazine
May Issue Now AvailableOn the road to learning
by Raju GandhiRefactoring to Modularity
by Kirk KnoernschildRESTful Groovy
by Kenneth KousenGetting Started with D3.js
by Brian Sletten