We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
August 13, 2024
Introduction to Rack middleware
EMIR VATRIC
đ Hi there!
What is Rack and how can we use it?
Being on the beautiful journey that is Software Development, and working as a Rails Developer, I often get curious about the different building blocks of this beautiful framework. Whenever I get time I like to go deep on one of these subjects, read about it and try to understand them, how to use them, and leverage that knowledge into producing better apps.
One of those important parts of Ruby on Rails certainly is Rack, and it is worth our time to go through. Learning about Rack, I have found more than a few excellent usages to better my skills and make my life easier.
What is Middleware?
This term gets used a lot, so much so that it is difficult to define it precisely but rather give it a few different definitions across the industry. We will try to focus on web development and usage of middleware mainly in Rails applications.
Middleware is a (loosely defined) term for any software or service that enables the parts of a system to communicate and manage data. It is the software that handles communication between components and input/output, so developers can focus on the specific purpose of their application.
In server-side web application frameworks like Ruby on Rails, this term is often used to refer to pre-build components or libraries like Rack middleware that can be added to the frameworkâs request/response processing pipeline in which data flows through a sequence of tasks or stages. Very common examples are logging, authentication, and the other, these tend to be the helpers that everyone needs across multiple applications, they basically intercept a request, do something with it and pass it on.
What is Rack?
Rack provides a minimal, modular, and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.
The characteristics of a Rack application is that the application object responds to the call method. The call method takes in the environment object as an argument and returns the Rack response object.
In its core, it is important to understand that Rack is not just a request/response filter it does so much more, Rack is a web server interface convention, a modular interface between web servers and web applications developed in the Ruby programming language.
Simple Rack App
In order to get a better understanding letâs take this nice and simple Rack app and go through the code to see what it does.
Weâll use WEBrick as rack compliant webserver, but any of them will do. Letâs create a simple web application that returns a JSON string. First, we need to create a config.ru file inside our directory, in our config file, we will have the following code:
class Application
def call(env)
status = 200
headers = { "Content-Type" => "text/html" }
body = ["This is our small Rack app."]
[status, headers, body]
end
end
run Application.new
Rack gem comes with little executable (command-line program) called rackup, so let's run it.
$ rackup
[2015-05-15 18:37:42] INFO WEBrick 1.3.1
[2015-05-15 18:37:42] INFO ruby 2.2.1 (2015-02-26) [x86_64-darwin14]
[2015-05-15 18:37:42] INFO WEBrick::HTTPServer#start: pid=17588 port=9292
Now our web application has started we can point our browser to localhost:9292
, and we should see text This is our small Rack app.
In Ruby on Rails, for example, the Rack requests hit the
and dependent on the env hash if any route matches it passes the env hash on to the application to compute the response, otherwise it immediately responds with a 404.ActionDispatch::Routing.Mapper
Rack the gem
Rack besides being a convention it is also a well-known gem, and it provides great functionality. In our aforementioned example, we have already used rackup command that comes with Rack gem.
Between the server and the framework, Rack can be customized to your application needs using middleware. Rack itself ships with quite a few middlewares from serving static files, setting default Content-Type header for responses to removing temporary files creating during a request. Please see the documentation for a detailed list of all middleware.
Community
There is also a great community around Rack gem that produces pre-built Rack components that help you deal with tasks like Authentication, Authorization, Caching, Decoration and so much more. For a full list please visit the Rack official documentation on Rack middleware.
But while we are here letâs take a look at a few examples.
SEO optimizations
SEO stands for âsearch engine optimization.â Itâs the practice of increasing both the quality and quantity of website traffic, as well as exposure to your brand, and these days it gets a lot of attention while building the products.
Recently my work buddy got the task to remove a trailing slash at the end of the URL, and we ware able to find a rescue in Rack component developed by its community.
Rack-rewrite is rack middleware for defining and applying rewrite rules, and it is super easy to use, for our purpose all we needed to do is to install the gem and call it inside our Application.rb.
config.middleware.insert_before(Rack::Runtime, Rack::Rewrite) do
r301 %r{^/(.*)/$}, â/$1â
end
Authentication
Doing the authentication from scratch can be difficult, and can cause unforeseen issues. Here we can get a lot of help from another Rack middleware, Warden, Warden provides a mechanism for authentication in Rack-based Ruby applications. Itâs made with multiple applications sharing within the same rack instance in mind.
Warden is designed to be lazy. That is, if you donât use it, it doesnât do anything, but when you do use it, it will spring into action and provide an underlying mechanism to allow authentication in any Rack-based application.
Conclusion
When I started writing and reading about this topic I genuinely didnât pay too much attention to the Rack, it is easy to take it for granted, but it makes our lives a lot easier.
While I am not a mad scientist, master, or senior I find it to be satisfying to write about topics in Rails, and especially about important parts of the Rails framework.
Hopefully, you will find this article interesting and helpful.