Thoughts on development, design and the world we live in.

Building Small Web Services with a Heroku and Unicorn Sinatra Template Project

By Curtis Jennings Schofield in Ruby, Web Development. Posted on May 15th

Using Sinatra Unicorn and Heroku (CEDAR stack) Together!

I have been asked

  • “How do I get started with Unicorn and Heroku?”
  • “How can I made a remote HTTP webhook?”
  • “How can I make a simple web service?” ,
  • “How can I get started with a free ruby hosting evironment?”

I want to take a moment to answer a few of these questions in a small package I have extracted from one of our small services. This package is avalible on https://github.com/blazingcloud/sinatra-with-unicorn with a MIT License

This blog posts assumes basic familiarity with the ‘git’ command : http://gitimmersion.com/

  • ‘$’ will prefix every command example:

Follow these steps to get started :

  1. Login or create a github.com account
  2. Visit https://github.com/blazingcloud/sinatra-with-unicorn in your web browser and ‘FORK’ the repository.
  3. Rename your fork by clicking the ‘ADMIN’ button
  • I gave mine a name off the top of my head : monkey-flyer-radio-protocol-death-claw-alpha

Follow these steps to set-up development:

I will follow each of the steps with the command that I used - replace ‘monkey-flyer-radio-protocol-death-claw-alpha’ with your project fork name

  • clone your repository

  • The repository has been setup to use bundler with a Gemfile - you will need to install bundler

  • With bundler installed you can use the Gemfile to manage installation of gems :

Follow these steps to change the default GET uri ‘/’ and see the results

  • open ./modules/site/main.erb in your editor
    • you can put html in here or text
  • run the server

  • Use ‘CTRL-C’ to stop the server

Follow these steps to add a post uri ‘/deathclaw’ and see the results

  • open ./modules/site.rb in your editor
    • this is a Sinatra Module - it has a get ‘/’ defined already.
  • add a line of code in the body of the class after the ‘get’ part

  • run your service again

  • run a post with curl on the command line in a new console window

Check in your changes

Release to Heroku (ON CEDAR STACK)

  • create a new heroku app ( i named mine )

  • release your code to heroku ( heroku creates a git remote ‘heroku’ )
    • you should see messages about heroku recieving your push and installing your gems via bundler

  • check your get

  • check your post


  • foreman start
    • this command will not reload your code changes - you will need to use ‘rerun’ if you want that.
    • rails does this for free - at the cost of increased learning curve
  • curl ARGS URL
    • no args
      • does a get
    • -d var=1
      • this tells curl to do a post with a post variable (unused, but simulates a post)

Post a Comment

Your email is never shared. Required fields are marked *