About the author

My photo

Java, Scala, and everything

Wednesday, November 30, 2011

Play! with Heroku

Recently, inspired by some talks on Devoxx, I decided to check Play framework and Heroku.

Play is another java web framework, but this one is heavily influenced by Ruby on Rails. Convention over configuration, little code necessary etc. Looks like Play is gaining momentum, version 2 (now beta) supports Scala and now it became part of Typesafe stack.

Heroku is another cloud. Well, quite different than AWS or GAE (actually, as far as I know, it works on AWS). Heroku is not only running applications, it is also able to build and deploy them. So to deploy your changes, it's enough to make git push.

So first, download Play 2 beta from this site. Unpack, add dir to your $PATH, so that you could use play command from anywhere.

Go to dir where you want to create the app and issue play new helloapp (where helloapp is your app name). Play should create helloapp directory. No go into it. You can notice that Play nicely added .gitignore file there, so when you create git repository here, unnecessary files won't be added. Play created fully functional, basic web application. You can issue play run in helloapp directory and the server is going to be started and application deployed. You can now see it on localhost:9000.

Now you'll need account on Heroku. Create one on heroku.com, it's easy as creating email or forum account. Then just activate it by email sent to you from Heroku.

For your app to be able to run on Heroku cloud, you'll need to add one more file. In the helloapp dir create Procfile file with content like this:
web: target/start
If you created your app with Play 1.x instead of 2.0, Procfile should be quite different:
web: play run --http.port=$PORT $PLAY_OPTS

To interact with the cloud you'll also need heroku-toolbelt. Get one from here.

Ok, you have your app and all the tools necessary to deploy it to the cloud. You'll also need git, which I assume you have already installed ;) As I mentioned on the beginning, it's necessary to push your app's code to Heroku.

Create git repository in the helloapp directory. Enter the directory and issue the following comands.
git init
git add .
git commit -m init

This commands created git repository in the directory, added current directory (helloapp) to the git index, and commited the changes to the local repository.

Next thing to do is creating new application on Heroku: heroku create --stack cedar from helloapp dir. This not only creates new application, but also nicely adds remote git repository. Heroku has few stacks. Stack is OS and stuff installed on it for our applications to run. For now, the only stack that supports Play is called Cedar. It's not the default one, so you need to tell Heroku explicitely that it should be used for your application.

It is also going to ask about your credentials to authenticate you on Heroku. It is done only once, and then your credentials key and token are stored on disk. If you want to delete them, just heroku auth:logout.

Ok, so you have application on Heroku, with git repository added to your remote ones, and you have one app locally which you want now to deploy to the cloud. Well, it couldn't be simplier:
git push heroku master

From now on it only takes some patience. Deployment takes time. When it's finished, it gives you the address, something like http://blooming-stone-5863.herokuapp.com deployed to Heroku in the console.

Go to this address and you'll see your app. Have fun!