Lately I have been playing a little bit with different Platform as a Service providers. So far, the most developer-friendly one seems to be Heroku. In particular, Heroku provides an incredibly straightforward way to get started. After creating an account on their website, you just have to install the appropriate gem:
sudo gem install heroku
heroku help will then show you all the possible invocations of the command line client, just to give you an idea of what you can do with it (everything).
Alright, that’s not particularly interesting without an application to play with, is it? So let’s create a new Sinatra application. In order to do that, we need a new directory containing a file like the following one:
# webapp.rb require 'rubygems' require 'sinatra' # Add other gems you might need here get '/' do "Your Sinatra app is working!" end
Also add a Gemfile with all the required dependencies, in our trivial example
sinatra is actually the only gem you need:
source :rubygems gem "sinatra"
Lastly, you need a
config.ru file where you can require our newly created Ruby program and specify that it is a Sinatra application. In this example, the Ruby file is called
require './webapp' run Sinatra::Application
A very useful program when developing a Sinatra application is shotgun.
gem install shotgun and you will be able to run your new shiny app by executing the command
shotgun. While developing on your machine, shotgun will take care of reloading the application for every request, so that you don’t have to restart the server after every modification to the source code.
bundle install to easily install all the required dependencies in your development environment, and
shotgun to start the app locally. Pointing your browser to http://127.0.0.1:9393/ you can check if everything works as expected. If nothing went wrong, you can create a git repository to track changes to the source code:
git init git add . git commit -m "Initial commit"
Perfect! Now, how do you create and deploy your application on Heroku?
heroku create git push heroku master
Heroku will automatically find out that you deployed a Sinatra application, it will install all the required dependencies and start serving requests. A few interesting commands you might want to run to see what’s going on are
heroku ps and
heroku logs. The client also provides a command,
heroku open, that opens your browser at the right production URL.
So, you now have a working application under revision control. Dependencies are handled by specifying them into the Gemfile, and the required gems can be installed locally using bundle. Shotgun is our friend when it comes to use the application for development purposes. A
git push heroku master is all we need to deploy a new version of the code.