Go to content
The Codest
  • About Us
  • Services
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
  • About Us
  • Services
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
2022-08-25
Software Development

The Role of Rack in The Ruby Ecosystem

Nicolas Nisoria

The Role of Rack in The Ruby Ecosystem - Image

Learn more about the role of rack in the ecosystem of Ruby from our our expert and up skill your ruby game.

While working with Ruby web frameworks it’s common to take things for granted. We know the framework will handle the HTTP requests and execute the middleware logic for us. As we get more curious we start wondering what is behind the scenes, there’s where we start hearing about Rack.

What is Rack?

The project is described as “A modular Ruby web server interface”. Rack is the interface that let us create web applications unifying the API for web servers, web frameworks, and middleware.

rack ruby scheme

As described in the above picture, Rack acts as a middleman between our Web Application and the Application Server, it wraps the HTTP requests in the simplest way possible.

Rack Application

A Rack application is a Ruby object (not a class) that responds to call. It takes exactly one argument, the environment and returns a non-frozen Array of exactly three values:

  • The status,

  • the headers,

  • and the body.

    You can find the detailed specification of a Rack Application here.

require 'rack'

class RackApp
  def call(env)
        status = 200
        headers = { 'Content-Type' => 'text/html' }
        body = ['<h1>My Rack App<h1>']

    [status, headers, body]
  end
end

Rack::Handler

Handlers connect web servers with Rack. Rack includes Handlers for Thin, WEBrick,FastCGI, CGI, SCGI and LiteSpeed. Each application server that supports Rack should provide a handler to create the connection (Puma has its own handler).Handlers usually are activated by calling MyHandler.run(myapp). A second optional hash can be passed to include server-specific configuration.

Using Thin application server

Rack::Handler::Thin.run(app)

The default file to add the configuration is config.ru and you can execute itusing rackup command in your console.

Rack Middleware

Rack allows us to create middleware applications (applications between our main web application and the application server). These middleware applications are chained together and executed sequentially.

Rack Middleware must implement all the specifications of a Rack Application and meet the following points:

  • It must be a class,
  • have an initializer that receives only one parameter (the main application),
  • and call the next middleware or the application.
class RackMiddleware
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
end
end

Rack into Practice

Now that we know the basics, we are ready to create our first Rack Application with Rack Middleware and run it using Puma (Application Server).

Install the dependencies

Make sure you have the rack gem and the puma gem installed.

gem install rack
gem install puma

Create the configuration file

First, we have to create a file called config.ru and this file will make use of the Rack::Builder DSL to run the application and add the middleware.

Add the Rack Application

Within the config.ru file, we will add the simple Rack Application we defined in the previous sections.

# config.ru

class RackApp
  def call(env)
    status = 200
    headers = { 'Content-Type' => 'text/html' }
    body = ['<h1>My Rack App<h1>']

    [status, headers, body]
  end
end

Add the Rack Middleware

Here we will make a small modification to our simple middleware and now it will add the server software to our HTML body after being executed.

# config.ru

class RackMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, body = @app.call(env)

    body << env['SERVER_SOFTWARE']

    [status, headers, body]
  end
end

Run the Application Server

As a last step, we will run the server and see our application running. Our config.ru file will look as follows:

# config.ru

class RackApp
  def call(env)
    status = 200
    headers = { 'Content-Type' => 'text/html' }
    body = ['<h1>My Rack App<h1>']

    [status, headers, body]
  end
end

class RackMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, body = @app.call(env)

    body << env['SERVER_SOFTWARE']

    [status, headers, body]
  end
end

use RackMiddleware
run RackApp.new

In the last lines, we specify the middleware using use and we run the application using run.We are ready to execute rackup in our console and see the server running. We can check the port where our application is running and we should see something like this after accessing it:

rack server text

Conclusions

Sometimes is good to go back to the basics and learn about the insights into the technology we work with. Learning Rack give us a clear overview of the architecture and reveals the “magic” behind the Ruby Web Frameworks.

cooperation banner

Related articles

Software Development

A Simple Ruby Application from Scratch with Active Record

MVC is a design pattern that divides the responsibilities of an application to make it easier to move about. Rails follows this design pattern by convention.

Damian Watroba
Software Development

GraphQL Ruby. What about performance?

GraphQL, like any technology, has its problems, some of them directly result from the architecture and some are identical to what we see in any other application. However, the solutions are completely different.

Tomasz Szkaradek
Software Development

Is Ruby on Rails a Good Technology to Build an MVP?

A minimum viable product (MVP) is one of the principles of the Lean Startup Methodology. The goal is to help the entrepreneurs start the process of learning as soon as possible. The product is not necessarily meant to be small or...

Nicolas Nisoria
Software Development

Top 5 Ruby Use Cases [UPDATED]

Have you ever thought what are your options with Ruby, and where can You use it best? Well, sky is probably the limit!

Pawel Muszynski

Subscribe to our knowledge base and stay up to date on the expertise from industry.

About us

Tech company specializing in scaling tech teams for clients and partners thanks to top-class development engineers.

    United Kingdom - Headquarters

  • Office 303B, 182-184 High Street North E6 2JA London, England

    Poland - Local Tech Hubs

  • Business Link High5ive, Pawia 9, 31-154 Kraków, Poland
  • Brain Embassy, Konstruktorska 11, 02-673 Warsaw, Poland
  • Aleja Grunwaldzka 472B, 80-309 Gdańsk, Poland

    The Codest

  • Home
  • About us
  • Services
  • Case studies
  • Know how
  • Careers

    Services

  • PHP development
  • Java development
  • Python development
  • Ruby on Rails development
  • React Developers
  • Vue Developers
  • TypeScript Developers
  • DevOps
  • QA Engineers

    Resources

  • What are top CTOs and CIOs Challenges? [2022 updated]
  • Facts and Myths about Cooperating with External Software Development Partner
  • From the USA to Europe: Why do American startups decide to relocate to Europe
  • Privacy policy
  • Website terms of use

Copyright © 2022 by The Codest. All rights reserved.

We use cookies on the site for marketing, analytical and statistical purposes. By continuing to use, without changing your privacy settings, our site, you consent to the storage of cookies in your browser. You can always change the cookie settings in your browser. You can find more information in our Privacy Policy.