Rails and Rack

Tram Ho

While learning and working with rails , we must all have heard of the concept of Rack .

So what is Rack ? How are request being sent to the Rails app handled?

Hi everyone, today we are going to learn about Rack issues.



Rack is defined:

Rack provides a minimal, modular, and adaptable interface for developing web applications in Ruby

It can be understood that Rack is an interface , a component placed between the webserver and the ruby framework, with the purpose of bridging the communication between the webserver and the ruby framework.

Request response with Rack

When a request is sent from the browser, the webserver (Puma, Unicorn, Webrick) will receive this HTTP request , then pass it to Rack , and the Rack continue to send it to the web app (Rails, Sinatra).

The web application handles the request , creating a response returns the user in the opposite direction:

So why do we need Rack , why webservers do not send request and receive response from webapps but through Rack ?

By using Rack to communicate between webserver and webapp, we can use many different webservers with many different web applications. For example, if you want to use Puma instead of Webrick, or use Sinatra instead of Rails, because webservers and webapps can communicate with each other via Rack :


Back to the request , when sent from the browser to the webserver, the webserver will convert the HTTP request into a ruby ​​hash: env , env which looks like:

env can be divided into 3 parts:

  • Request headers:

  • Server info:

  • Rack info:

So how is this env variable handled in Rack?

Rack application will receive this env variable. A Rack app needs a method call , and returns an array of 3 elements:

  • The HTTP response code
  • A Hash of headers
  • The response body

Rack middleware

Each Rack middleware is a rack app , with different purposes, performing different functions with the request sent or the response returned.

Print rails?

Rails is rack application, a final application built by a series of middleware with different purposes. $ rake middleware will list the middleware in the app:

Some middleware:

  • Rack :: Sendfile : handles static files under the public directory
  • Rack :: Runtime : Adds an X-Runtime field in the returned response header, indicating the processing time for the corresponding request
  • Rack :: MethodOverride : sets the value for the HTTP request method based on the input “_method” in each form generated by the rails form helper.
  • ActionDispatch :: RequestId : assign a unique ID to each incoming request
  • ActionDispatch :: ShowExceptions, ActionDispatch :: DebugExceptions : catch exceptions and display custom pages:
  • ActionDispatch :: Callbacks : provides before / after callbacks
  • ActionDispatch :: Flash : save flash, and remove old flash in the session
  • ActionDispatch :: Session :: CookieStore : stores session in cookie
  • ActionDispatch :: Cookies : store cookies in the browser via Set-Cookie header
  • Rack :: Cache : supports rails with HTTP Caching

request to, middleware in turn called in order, finally to RailsApp :: Application.routes, find the corresponding controller, and return the response

Above are some of my findings on Rack , thanks for listening 💥

Share the news now

Source : Viblo