Sunday, November 23, 2008

Sinatra is Kind of Cool - Passenger Tips

Sinatra is another very cool Ruby web framework that packs quite a punch in what is currently only 1,576 LOC. It has pretty decent routing, filters, layouts, and more. If you are building something that functions primarily as a web service it would definitely be an option worth checking out.

Sinatra is Rack compatible and thus deploying behind Phusion Passenger works well. Unfortunately, the documentation on the Passenger site leaves a bit to be desired and may leave you scratching your head as to why your Sinatra application isn't working correctly. Here is a sample script that will bring up your application behind passenger with an explanation coming after the code:

First off, my provides the ability to "freeze" to a certain version of Sinatra with the $:.unshift line near the top of the file. This line just checks for the existence of a vendor/sinatra directory in your application and if present pushes its lib directory to the front of the load path. The setup of the default options is where I add some information not given by the Passenger instructions which will otherwise cause your application to fall down go boom - that is you must tell Sinatra how to find its views directory (so that you can use views) and its app_file (so that it will reload correctly when running under :development mode). Also notice how the environment is picked up from the RackEnv variable coming from the conf file. The real guts of the application are defined in app.rb which is required at the bottom of the file.

This is my first post using gist to host code snippets and it seems to be a winner. I think I will also start hosting my Javascript example code straight out of gist raw as well.