“Rack is a beautiful thing” - that’s how Ryan Bates started his Railscast about Rack middleware in 2009 and I totally agree with him over 7 years later. Rack is indeed a beautiful thing and to be honest I’m happy I started my Ruby web development adventure from Rails 3.2. Why? Because it has already had Rack support! Of course, it’s not a perfect solution for every web app (is there any?!), but it is widely used thanks to its simplicity and power. By the way, I have recently started a new project based on Rails 5.0.0.beta1 and it still uses Rack. Do you want to know why Rack is so awesome? Go on!
Rack is like a good waiter. It makes a Ruby web developer life easier by taking care of the requests like a waiter cares about the dishes in a restaurant. Thanks to that you can spend more time on coding your application rather than configuring the server environment to handle the HTTP requests properly. The best one-sentence Rack description comes from its homepage:
"Rack provides a minimal interface between webservers that support Ruby and Ruby frameworks."
It is really minimal. Everything you need to know is that your application have to respond to call method, which takes in the environment hash and returns 3 elements in an array: code of the HTTP response, hash with headers and the response body. The last element (response body) must respond to each method.
To setup a dead simple Rack app (working in the browser!) just make a sample.ru file:
Why do we use a Proc here? Because it answers to the call method. This Proc contains the elements mentioned before, so the next step is to run the following command from the same directory you have your monster app.
Voila! Launch your browser and visit localhost:9292 to see the “Rack and Roll” now! Of course you can specify the port by passing an optional parameter in the command line:
and your app is available under localhost:5678. If you think that rails server command can be replaced with rackup in your app, I have a news for you - you’re right! Isn’t it beautiful? :-)
Rack under the hood
Rack runs under the hood of most of the Ruby web frameworks, not only Rails. Sinatra, Padrino, Cuba or Hanami (previously Lotus) also use Rack. Just take a look on ActionDispatch code shipped with Rails:
1 2 3 4 5 6 7 8 9 10 11 12 13
it returns the array that Rack is waiting for, just like Sinatra:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Of course - you can build your Ruby web apps without using Rack, but the reason it’s so commonly used is that it follows a few simple rules no matter which webserver and framework you use. There’s no need to spend extra hours (or days? or weeks?) dealing with the Ruby Net::HTTP library to serve the responses or the requests up and down the stack. Rack does it for you. For free!
Rails routes are Rack endpoints
If you’re still reading this, it’s worth to mention that all the Rails routes are Rack endpoints. You can write every single route in your app like that:
Rails just use a shorthand syntax to make it easier for you to create the routes. Sinatra works quite similar with its mount method. It all ends up with a Proc or Lambda. Need an evidence? Try that in the Rails console:
and you will get something like:
it’s ready to call!
Your own middleware
One of the most powerful “features” Rack provides is a possibility to jump in between the server and application. That allows you to manipulate with HTTP request/response before passing it further down/up the stack. Visit the root folder of one of your Rails apps and type
in the terminal. You’ll see all of the things being processed right between your app and the web server. You’re very likely using some of the Rails standards like ActionDispatch::Cookies or Rails::Rack::Logger and it’s perfectly fine. Now think which of your apps does not use e.g. flash messages and you can remove the ActionDispatch::Flash from you middleware. Your app will thank you for that!
Do you have an idea to customize your requests flow? Go for it!
Rack is great not only for that it makes our life easier when it comes to handle the HTTP requests and responses. I feel like Rack helped the Ruby web development community to grow by unifying the API between the servers and the frameworks. Thanks to that we not only have Rails, we have much more frameworks to explore. Thanks to that we we don’t need to use the default Webrick in development (actually Puma is now default for Rails5.0.0.beta1), we can use Thin, Unicorn, Puma or Passenger. It all still makes the community grow and help devs to experiment and choose better. Of course - it’s not only Rack merit, but I think Rack has made a solid contribution to the Ruby/Rails family.
Rack is beautiful! :-)