With all the Ruby frameworks popping up, we’re starting to see some similarities. All of them provide something new or unique but one part of their code is always the same. The part that plugs it into a web server. Ultimately, all web servers have to support all frameworks and vice-versa. That is a lot of duplicated code! That makes me yell, running in circles, waving my arms: not DRY, not DRY, not DRY!
What?
Rack (not the iRack) by Christian Neukirchen, solves this problem.
From Rack website:
Rack provides an minimal interface between webservers supporting Ruby and Ruby frameworks.
Rack looks like this in my head:
Handlers on the left (web servers) serve requests to Adapters (frameworks) on the right.
(Don’t ask me what the rabbit is doing there, must be important)
Run Forest!
But Rack can do a lot more then plug Ruby framework into webservers. It is a framework in itself.
When you install it:
sudo gem install rack
You get the rackup
command. Which lets you start your app on any supported web server.
require 'rack/lobster' run Rack::Lobster.new
– config.ruStart your app:
rackup -p3000
then browse to http://localhost:3000, oh the cute lobster!To run it on other web servers (mongrel by default) play with the
-s
option.It’s a framework I said
The Handler API is only 1 method:
call(env)
. Which allows you to use proc as application.
app = proc do |env| [200, { 'Content-Type' => 'text/html' }, 'no pepper plz!'] end run app
– config.ruThat will return a 200 OK response with the text/html content type and the body no pepper plz!.
env
contains the request parameters, so you could play with the QUERY_STRING or wrap it inside aRack::Request
and get a request param value withrequest.params['name']
.Plug
You can use middlewares to filter the process. For example, you can add logging and detailed exception support with those 2 lines in your config.ru file:
use Rack::CommonLogger, STDOUT use Rack::ShowExceptionsYou can also validate your application, the requests and responses according to the Rack spec automatically using the
Rack::Lint
middleware.(There’s also a
Reloader
,Static
,Cascade
,File
and much more middlewares, check the doc)You can also have fun with urls:
map '/files' do run Rack::File.new('.') end map '/lobster' do run Rack::Lobster.new endCheck it out!
Rack is the best example of a very well design Ruby library. The code is simple, well separated and yet, easily extensible, it’s beautiful! I encourage you to check out the code but mostly to use it if you’re building any Ruby web framework (like we need other one!).
hmm… server …. deployment …
eager to see what you guys have done.
Dog…Rabbit Rack::Lobster … it’s the time we are going banana
:O)
hey thx for the comment Philippe,
Just to be clear, I’m not involved in the Rack project, it was created by Christian Neukirchen, I just though it was very cool.
Lobsters are so cool, I know!
I am .Net developer, but i’m reading blogs about Python, rails, java, and everything else. I noticed very quickly that the same ideas float everywhere, no matter what is the language syntax or platform used.
All communities has all modern tools and concepts like rails-like web frameworks, ORM solutions, logging frameworks, etc.
That shows a very healthy spread of idies, which is good ™ 🙂
Indeed this project was inspired by Paste written in Python.
What other project did it remind you from?
The rabbit? The dog is watching the rabbit; it’s a metaphor.
Pingback: decodeURI » Creating a Rack Middleware for Minifying Your Javascript files
Its not beautiful on the inside haha have you looked at the source? VERY messy
Pingback: James Robertson (jrbookmarks) 's status on Thursday, 03-Sep-09 16:17:41 UTC - Identi.ca
Pingback: James Robertson (jrbookmarks) 's status on Thursday, 03-Sep-09 16:18:27 UTC - Identi.ca
There’s a Rack coding contest starting now if you want to enter. It’s at http://www.coderack.org
Some great prizes and it promises to be a lot of fun.
Pingback: Creating a Rack Middleware for Minifying Your Javascript files « decodeURI
Pingback: Rack - What the hell?
Great Submit, Many thanks
Witty! I’m bookmarking you site for future use.
I’ve just been promoted to Level 162 in #MobsterWorld. Beat me in the game! http://t.co/DL3UwuhH
Pingback: Gaptek
magnificent post, very informative. I’m wondering why the opposite experts of this sector do not understand this. You must continue your writing. I am confident, you have a huge readers’ base
already!
wonderful inspiring collection.gorgeous created designs here!Thanks for sharing.