This post is just about me sharing some of the stuff I have looked at in the open source community lately. There might be some ruby stuff in here. First out is rack.

Rack

Imagine for a minute that there were a lot of different web servers out there like IIS,  Apache and perhaps even Kayak. Also imagine that there were a lot of different web frameworks out there, like Fubu MVC, ASP.Net Web Forms (don’t get me started) and Asp.Net MVC.

Wouldn’t it be cool if we could put any web server together with any web framework just by putting a common abstraction in between ?

The web frameworks would only need to implement an adapter working with the abstraction and the same for would go for the web server. To get n web frameworks working with m different web servers, we would only need to implement n+m adapters as opposed to n*m without the abstraction. We could pick our favorite server and our favorite web framework and start developing with a smile on our face. (Ignore for a minute that this is not really possible with the servers and frameworks I just mentioned)

What would this abstraction look like ? Well, a web server is at its hart a very simple thing. You get a request and return a response. A response is simply a status, a set of headers, and a body.

In .NET a very simple implementation of a web “framework” adhering to the abstraction would look something like this.

public Response Call(Environment env)
{
  return new Response(
    status: 200,
    headers: new Dictionary<string, string> { { "Content-Type", "text/plain" } },
    body: new List<string> { "Hello world" });
}

or the more terse ruby version

def call(env)
  [200, {"Content-Type" => "text/plain"}, ["Hello world!"]]
end

Now I finally came to the point of this entire rant. In ruby they actually have this abstraction in place. It’s called rack. You can mix and match web servers and web frameworks to make your coding life a dance on roses.

I like it.

You would think the story ends here, but it doesn’t. There is also something called rack middleware. It takes advantage of the common abstraction between the web server and the web framework and offers you the ability to put logic in between the two. It is entirely possible to create rack middleware for debugging, logging, authentication, the list goes on. You now have reusable components for your web site that is independent of both the web server and the web framework you have chosen. Cool huh?

I like it.

(This part of the post was mostly inspired by this introduction to rack)

Sinatra

Sinatra is a rack enabled web framework. Its implementation is very small and the sample application is even smaller. I will not say much about Sinatra, because the web site says it all.

require 'sinatra'
get '/hi' do
  "Hello World!"
end

Ahhh, the beauty of simplicity. Describe the URL, define the action, and you’re done.

Since Sinatra supports rack, you get the power of rack middleware in addition to the simplicity of Sinatra. I like it.

Tilt

Any serious web framework needs a nice templating engine. In ruby you have a lot of choises like erb, haml and my current favorite mustache. It would be a pain (in the ass) to implement support for all these engines in every web framework, so the ruby guys have (mostly) agreed on tilt. Tilt is a common abstraction that goes between the web framework and the templating engine. Sounds familiar ? To use a tilt enabled templating engine, you need to supply the template and some context for the template (the data). Easy easy.

template = Tilt::ERBTemplate.new('templates/foo.erb')
joe = Person.find('joe')
output = template.render(joe)

Nice stuff.

Well, that’s it for now. See you later.

F Share