30 October 2010

static file handling in Sinatra

I've been using Sinatra for a bit at Forward, here's what I like about Sinatra.
  • Its interface for http integration is a Rack. Sinatra#call(env) that returns [status, headers, body] How simple is that? It's hard to beat.
  • Any Rack based web app is so easy to test. Out of container test is baked in from the birth.
Then I found the default static file handling in Sinatra ignores before_filters and after_filters defined by its beutiful DSL.

How do I set Cache-Control headers for static files?
 
Enter Rack middleware which is essentially a filter. All I had to do was to write a simple CustomHeaderFilter riding on the same simple Rack interface, another happy day :)