Sinatra and Heroku: the elevator pitch

Recently, I needed to upgrade an old ASP.NET 2.0 site I’ve maintained for a family member.  I built the original site back in 2000 or so with ASP and FrontPage (had the FrontPage Bible on my desk for that one).  I later upgraded the site in 2005 to ASP.NET 2.0 with semantic HTML, XHTML and all that goodness.

But I had been reticent to touch it for the smallest requested change.  Not because the change was difficult, but because it was so difficult to deploy an ASP.NET 2.0 application.  Sure, there’s some web deployment project or something, but it was still annoying synchronizing production with development.

So I set about finding something better.  I didn’t need anything in ASP.NET, as this was simply a brochure site.  I needed to be able to upload content, theme and style it, and most importantly, control HTML, CSS and JavaScript.

Choosing Heroku

I had heard about Heroku a while back, but hadn’t given it much thought as I wasn’t building any web sites.  I wanted to solve my deployment problems by simplifying deployment as much as possible.

Heroku is a cloud platform for hosting Ruby applications.  What’s fantastic with Heroku is the insane simplicity of every interaction.  It’s got a fantastic website, but the really interesting piece is that to deploy, you simply do a “git push”:

$ git push heroku master

There are no deployments, there is simply a push of my local git repository up to my Heroku repository.  One way to handle deployments is to put a bunch of tooling support in.  Or, another way is to remove the need to care about deployments and I just push my entire site up.

The coolest thing about Heroku is the entire platform has embraced “the Ruby way”.  If you need a specific plugin, it’s just gem packages.  Rake is available for database migrations, and Rack-based web frameworks are supported (including Sinatra).

Heroku is like many other cloud-based services like github, where the basic features are free, and add-ons cost extra.  Looking at my requirements, I didn’t need any addons, so my hosting cost is ZERO.  All I needed is a domain.

Choosing Sinatra

When it came time to actually building my application out, I first started with Rails.  When it came time to build my model, I stopped.  I didn’t have a need for any model, this application didn’t have a database to begin with, so why would it need one now?  So I ditched Rails, it wasn’t what I needed.

All I needed was to have some static routes and static content that I could have complete control over.  There was some dynamic information shown, but nothing that required a database.  I didn’t even need controllers.  That’s just too much weight to drag around.  All I really needed was to link up routes to content.

For these needs, Sinatra was the perfect fit.  I have one code file that defines my routes and what to do to handle them:

require 'rubygems'
require 'sinatra'
get '/' do
  'Hello world!'
end

If I want to show a view, it’s:

get '/' do
  erb :index
end

I then can use erb as a view engine (or Haml or whatever).  But I don’t need an IDE, projects, solutions or any junk like that.  I just stick with a text editor like e, and I’m done.

Adios, ASP.NET

Unless I’m building on top of an existing application or are in an IT environment, I can’t really fathom why anyone would choose .NET for static websites these days.  Platforms like Ruby and PHP are cheaper and simpler to develop, deploy and manage with fantastic hosting services like Heroku.  I don’t have to open an IDE, which at that point is just to heavyweight for what I’m trying to do.  Command line and a good text editor is much easier to deal with.  Otherwise, my browser (Chrome) is my IDE.

My hosting cost wasn’t that much to begin with, but it’s now zero.  I don’t even have to worry about deployments, I just push my entire repository up to Heroku with one git push and it manages the rest.  If you’re about to create a new public site or upgrade an existing site, I highly recommend giving Heroku a look.  If nothing else, it will depress you on how easy web site development and deployments can be.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in Ruby. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Roco

    +1 for this post.

    The elegance, simplicity, and productivity (3 adjectives!) of these Ruby based tools is outstanding.

  • http://chrismissal.lostechies.com Chris Missal

    I’ve used both Sinatra and Heroku just a small amount, but they’ve been fantastic in that tiny amount of time. You’re entirely correct, Jimmy, if you’re building a mostly static site these days, this is a perfect combination.

  • http://cre8ivethought.com/blog/index Mark Nijhof

    +100 on Heroku

    And if you ever need domething more then Sinatra but still feel Rails is to much or to opinionated then you should take a look at Padrino!

    -Mark

  • http://cre8ivethought.com/blog/index Mark Nijhof

    Oh btw it is perfect for non static sites as well ;-)

  • http://www.newitup.com/ Dan

    I recently built a pretty simple ASP.NET MVC app and I’ve been looking for cheap, simple hosting. I’ve seen many similar blog posts to this one praising the simplicity of Heroku. I think I’m going to use it as my excuse to dig in and learn RoR, partially because I need a kick in the butt to do so and partly because Heroku just seems like such an easy way to host a site. Thanks for further pushing me in that direction.

  • http://blog.timwingfield.com Tim Wingfield

    I’m on a similar adventure. Started a side project in ASP.Net MVC but after a weekend of work had nothing the user could see. Switched to Rails and I got something out to the users much quicker, but once I got further into it I realized there was a lot more in Rails than I needed. Switched to Sinatra, and haven’t looked back.

    The only thing I’ve got going that you don’t here is I’m using MongoDB and MongoHQ. The MongoHQ experience was on par with Heroku.

  • http://javiercrespoalvez.com Javi

    I also used Heroku + Sinatra for a static site recently.

    A trick for Heroku:
    If the site is not visited very often, any hit after a long period of inactivity will take very long to load.
    There are some sites in the net that check every n minutes if your site is running so this can be used to warm it up. I set it up for free with mon.itor.us

  • http://lazycoder.com Scott Koon

    I don’t understand why you would use ANY web framework for a static site? If you are already writing the templates, just put the content in the html page. If you need to be able to change the content or let the user interact with the site, then it’s not a static site.

  • DaRage

    >> But I don’t need an IDE, projects, solutions or any junk like that

    but how do you debug? can you set breakpoints in e?

    i can’t imagine developing without debugging.

  • DaRage

    I use google cloud for my static sites. it’s free and very easy to setup.

  • Roco

    >but how do you debug? can you set breakpoints in e?

    Chances are, if you are building a simple static site like Jimmy is talking about, you probably aren’t spending much time in a debugger – regardless of the platform.

    There are plenty of debugging options for Ruby, but in this case you probably don’t need to go there. Also, good testing practices can eliminate a substantial amount of the knee jerk reaction to rely on a debugger.

  • http://ampgt.com Scott Bellware

    DaRage,

    I’ve been coding in Rails for three years and I’ve only needed to use the Ruby debugger on one occasion – and the problem turned out to be an obscure edgecase bug in an older version of ActiveRecord. It’s there when you need it, and if you want it integrated with an editor, you can use RubyMine from the makers of ReSharper.

    Language and framework elegance go a long way to defeating the need for a debugger – even if you don’t want to take on the mantle of testing.

  • http://ampgt.com Scott Bellware

    Jimmy,

    Check out this article for an idea of how far you can go with Sinatra:
    http://rubylearning.com/blog/2010/09/21/writing-modular-web-applications-with-rack/

    Like Mark said, if you like Sinatra, spend an afternoon with Padrino. It’s really shaping up to be an awesome next generation.

  • http://ampgt.com Scott Bellware

    Scott,

    Dynamic is the new static :)

    I think the vernacular here has changed to indicate that if there’s no external data integration that it’s static, rather than the tried and true definition of static web meaning that all content is served from disk as-is with no dynamic content construction from parts, layouts, etc. This “static” isn’t the “static” that we usually think of, as with requests for assets that would be served from a static asset server like Apache, nginx, etc, rather than being passed on to a Ruby process.

    It’s not technically accurate, but not entirely uncommon for people using Rails, Sinatra, or any host of web frameworks to refer to sites as “static” when built with dynamic construction tools but that don’t have data integration.

    In response, “static generation” is often now used specifically to refer to true static content that is pre-generated with tools like Jekyll or webgen.

    The wheels on the bus go `round :)

  • http://ampgt.com Scott Bellware

    Jimmy,

    PS: If you’ve got app warm-up latency, and if the content is truly static (unchanging), you can set the cache-control header on the response to something very large.

    You app on Heroku is fronted by Varnish, which will respond with the cached content rather than hitting your Sinatra app.

    The best part of the Heroky deployment story is that all static caches in Varnish are purged when you push to your heroku remote.

    Heroku has a doc on this that talks about Rails, but you should be able to do the same. You might need to use a Rack extension, but it’s not hard to do with Sinatra since the framework sits quite close to Rack.

    See: http://docs.heroku.com/http-caching#caching-dynamic-content-by-age

  • http://www.lostechies.com/members/bogardj/default.aspx bogardj

    @Scott

    Thanks for the links! All stuff I wish I looked at a year ago. The only thing I miss from VS are the tiny enhancements for editing HTML (close tags automatically, etc.) This may be me just not using the editor appropriately, though.

  • Roco

    @bogardj,

    e texteditor has tag/code completion. Look into the bundles.

  • Thedric Walker

    @bogardj If you are using the e, vi, or emacs, you should look into Zen Coding
    http://code.google.com/p/zen-coding/

  • Steve

    @bogardj,

    Use Haml, no need to worry about end tags every again! :)

  • lubos

    “git push” will work very well in ASP.NET environment as well. in fact, deployment of all my asp.net webapps is as simple as doing “git push”.

  • http://www.lostechies.com/members/bogardj/default.aspx bogardj

    @lubos

    Man, that’s crazy, cause none of mine can do this. What’s your secret?

  • http://devlicio.us/blogs/christopher_bennage/ Christopher Bennage

    I started watching the Tekpub series on Sinatra last week. That combined with your story here, and I really want to start digging deeper. Thanks for the post.

  • http://www.corebvba.be/blog tom janssens

    @bogardj

    Rob Connery wrote an article on git for asp.net mvc deployment: http://blog.wekeroad.com/2009/11/23/deploying-a-web-application-with-git-and-ftp

  • sam

    why wouldn’t you just get your family member on wordpress and completely remove yourself from the process?

  • http://www.lostechies.com/members/bogardj/default.aspx bogardj

    @sam

    It’s not _zero_ code, or that would be a fantastic option. It’s juuuuust enough code (jQuery + Ruby) that requires something extra.