Create an online webservice fast with Sinatra and Heroku

Create an online webservice fast with Sinatra and Heroku

So you want to explore the posibitilies of the web, and you want to look the static HTML files served from a web server. Here is a short introduction to how you get a dynamic webserver up and running as fast as it gets utilising the Sinatra web-framework and the hosting provider Heroku. Heroku has already have gained cred in the developer society, and Facebook is now recommending Heroku as PaaS (Platform as a Service) when you create services. It is super-easy to get started, and comes with lots of instant gratification.

Heroku_serious_mem

Sinatra and Rails

Sinatra is a lightweight web-framework using Ruby with Rack. I have earlier looked at Ruby on Rails, which is also a great framework based on the same platform. The main difference is the complexity of the two systems. Rails comes with a great Model-View-Controller structure, database mapper and lots of neat stuff, but sometimes you just want simplicity, and Sinatra got simplicity written all over. Yes, you may do a lot of advance things in Sinatra, and you can make something really simple in Rails, but why not harness the nature of these two frameworks. If you are going to make something complex and wants a strict convention-over-configuration approach go for rails, if you just want something really simple, clean and clear let’s get started with Sinatra.

Installation

I expect you to have ruby and it’s gem package version system installed. If not you will have to install these. I would recommend you to also look into RVM, so that you can have more ruby versions installed with their gems, and in this manner retain the software versions of your platform and gems when you upgrade other applications.

All you have to do is download the package ‘sinatra’. Just “gem install sinatra” into your shell and et voila. You now have installed Sinatra. You can verify this by opening the ruby in an interactive shell with the command ‘irb’, and then require the package “require ‘sinatra'” in the irb-terminal.

You will also need to install heroku. The process is as easy as installing sinatra. Just type “gem install heroku” into the shell.

You would also need to install git – the version controll system – and sign up for Heroku.

Installing Git

The version control system system Git is gaining a lot of momentum. The website Github is based around git, and Heroku uses it for pushing local files to the web server. A fun fact: Git has also inspired a TED speech by Clay Shirky. You can download Git from here.

Sign up for Heroku

Just like installing Git is easy, signing up for Heroku is even easier. Sign up through Heroku’s website and follow the instructions. You will also need to download a heroku gem which makes interaction through the terminal quick and easy.

You also need to create a pair of SSH-keys and exchange these with the server for authentication when you are uploading files. Here you find a tutorial for creating the keys.

Creating a Sinatra application skeleton

What makes Sinatra a very convenient tool is how lightweight it is, and still expendable. When you start you only need a couple of files, and from there you expand the application to accomodate the level of complexity you need. I like to use Sinatra with dead simple small applications.

To begin creating a Sinatra app, first create an empty folder. I use the computers Command Line Interface to use this (on a Mac this is called Terminal and can be found in the Utility folder). You can create a new folder by using mkdir command with the name of the folder-to-be as the only argument: ‘mkdir myfirstsinatraapp’

Navigate into this folder by using the cd command. For the most convenient *nix commands please see this introduction.

Put the Sinatra application under Git version control

In the folder, initialise git with the command “git init”. You now have an empty git repository.

Create the app

The only thing you will need to begin with is a config.ru, Gemfile file and an app.rb file. An easy way to create these are with the “touch filename” command where filename is the name of the desired file.

In the config.ru file, require the app.rb file, and call the “run Sinatra::Application” command. The file should look like this:

require './app'
run Sinatra::Application

 

The other file you would need to create is the app.rb. This file would act as your main file for your application. Where you can generate routes and fill them logic.local

At the top of the file, require sinatra and create a route to the index path:

#!/usr/bin/env ruby
#encoding: UTF-8

require 'sinatra'

get '/' do
"Hello world"
end

 

The ‘/’ represents the root of your web-app, and once you go to this page you will be prompted with the text “Hello world”. Don’t take my word for it, try it yourself.

You will also need a Gemfile, in which you list the gems used by your application and where to get them. Since our application is super-simple we only need to list Sinatra. After writing and saving the gemfile. Install the gems by typing ‘bundle install’.

source 'https://rubygems.org'
gem 'sinatra'

In the folder with your files, type “ruby app.rb“, this will start a web-server with your application. The default location is localhost:3456, but this will be prompted in the terminal. Open a web browser, and type in the address and port-number to your web-server. You should now see the plain text “Hello world”.

A very simple output
A very simple output

Commit and push your application to Heroku

To publish your application to Heroku you will need to add all files to the empty git repository we created above. Add all files with the command “git add .” (The dot represents the current directory). Check that all files are staged with “git status“. Commit the changes with “git commit -m ‘wohoo. going live’ “.

Now, all the code is in the git repository and ready to be uploaded to Heroku. Write “heroku create” into the shell. After the command is executed a URL to your service on heroku should have been created and displayed by the command, the command should also have created a remote repository in your git file. This is transparent to you as a user, but you can verify by typing ‘more .git/config’. A line with ‘[remote “heroku”]’ and a couple of lines under this should have been added.

When this is done, you are ready to push your code onto Heroku. Type ‘git push heroku master‘ to make your local code available from the internet. As you add files and alter code, add these to the git, commit and push them to Heroku.

Leave a Reply

Your email address will not be published. Required fields are marked *