If you aren’t familiar with the term DSL, it stands for Domain Specific Language. It is a programming language with a higher level of abstraction optimized for a specific class of problems.
A DSL uses the concepts and rules from the field or domain. For example, Ruby on Rails provides a DSL for building web applications.
Rack provides such a DSL consisting of the following methods:
run
: It takes a Ruby object responding to the call method as an argument and invokes the call method on it. We used this method in our example at the beginning of this post.map
: It takes a string and a block as parameters and maps incoming requests against the string. If it matches, Rack will run the block to handle that request. This is similar to how routing works in Rails.use
: It includes the middleware to the rack application.
Additionally, the Rack::Builder
class helps you with iteratively composing Rack applications.
For example, here's an example config.ru
file using the above DSL:
# config.ru
require_relative './app'
app = Rack::Builder.new do
use Rack::ShowExceptions
use Rack::Logger
map "/welcome" do
use Rack::Lint
run App.new
end
end
run app
This DSL tells Rack to use the Rack::ShowExceptions
and Rack::Logger
middleware before using our App
.
When a request comes from the web server, Rack will first use the exception-handling middleware, then the logging middleware, and finally run the app.
The app will process the incoming request, and return the response, which will again be examined and processed by the middleware stack, this time in the reverse sequence.