What is Rack in rails…?

In simple words, Rack is the bridge between your app and server. It’s is a nice Ruby-fied replacement for CGI. Rack middleware is more than “a way to filter a request and response” and a Ruby package that provides an easy-to-use interface to the Ruby Net::HTTP library.

What is Common Gateway Interface(CGI)…?

CGI is an interface which tells the webserver how to pass data to and from an application. More specifically, it describes how request information is passed in environment variables (such as request type, remote IP address), how the request body is passed in via standard input, and how the response is passed out via standard output.

In rails, Rack acts as CGI.

Basic understanding of Rack.

Now you are running a server on http://localhost:3000/messages rails app on your machine. The ‘/messages’ is going to go to your Rails server asking to show all messages. Behind the scenes, this HTTP request that the browser sends looks like this:

// Request by the browser
GET /messages HTTP/1.1
Host: localhost
Connection: close

And, the response sent by the server looks like:

// Response by the server
HTTP/1.1 200 OK
Content-Length: 25
Content-Type: text/html
.....
.....

Why RACK…?

The problem is the server understands HTTP request and does not know how to communicate with your rails app. Your Rails app does not understand browser requests directly. You need to translate it in a way that he can make sense of it, work with it and then give you a response. This is why we use RACK.

So Rack is all about what the server should send to the app and what the app should return to the server. That’s it.

How Rack communicates between server and app…?

First the server will convert the HTTP request to a simple Ruby Hash , as shown below:

// Server to Rails app
env = {
  'REQUEST_METHOD' => 'GET',
  'PATH_INFO' => '/messages',
  'HTTP_VERSION' => '1.1',
  'HTTP_HOST' => 'localhost',
  ...
  ...
}

The above env is sent to the app. The app does all necessary work to based on the information it got from the above env variable and returns response to the server. The response would be cleary understood and specified by Rack, so that the server understands it.

The app response back to the server is a simple Array. This array has 3 elements in it. First is the HTTP status code of the response, second is a Hash of HTTP headers and the third element is a body object.

// Rails app to server
[
  200,
  {
    'Content-Length' => '30',
    'Content-Type' => 'text/html'
  },
  [
    '',
    '...',
    ''
  ]
]

The server takes the above array and convert it into a valid HTTP response and send it to the browser.

Now there are certain rules about what things the Ruby app should compose of, to be able to work with that env variable from the server. In its most basic sense this is what the Ruby app should contain:

class App
  def call(env)
    [
      200,
      { 'Content-Length' => 30, 'Content-Type' => 'text/html' },
      [ "", "...", "" ]
    ]
  end
end

The app should implement a method named call which accepts a parameter env. This method should return the resultant Array we talked about. Any app that confirms to this rule is a Rack application.

More about Rack:

Your Rack server object takes in an environment hash. What’s contained in that hash? Here are a few:

REQUEST_METHOD: The HTTP verb of the request. This is required.
PATH_INFO: The request URL path, relative to the root of the application.
QUERY_STRING: Anything that followed ? in the request URL string.
SERVER_NAME and SERVER_PORT: The server’s address and port.
rack.version: The rack version in use.
rack.url_scheme: is it http or https?
rack.input: an IO-like object that contains the raw HTTP POST data.
rack.errors: an object that response to puts, write, and flush.
rack.session: A key value store for storing request session data.
rack.logger: An object that can log interfaces. It should implement info, debug, warn, error, and fatal methods.

One Response to “What is Rack in rails…?

  • Hello there! Marvelous posting! I appreciate strategy that you reviewed What is Rack in rails…?.
    I’ve never thought about What is Rack in rails…? in this way.
    Say thanks a ton website author, it actually was very interesting to know .

    You can’t say everyone have the ability to come up with as nice
    as this author associated with this blog. Nearly
    everybody battle while using freelance writing .
    Thinking of the idea, Naturally i couldn’t see how to deal with this challenge without websites reviewsforstudy.com.
    In-depth overviews of the largest the internet producing corporations can be useful virtually
    every internet user to buy an effective reputable company

Leave a Reply

Your email address will not be published. Required fields are marked *