Rack interface b/w Ruby-based application and Web server

Tram Ho

As a Ruby developer, sometimes you often hear the term Rack application, or in your projects, more or less you have come in contact with Rack, so what is Rack, why should we understand? About it, this article will help us answer the following questions:

1, What is Rack? 2, What is Rack middleware? 3, Where to use it Rack?

What is Rack?

With Rack, it provides a minimal interface between webservers and Ruby support frameworks. In simple terms, Rack stands in the middle of a Ruby application and web servers to help them interact with each other.

Creating Rack application

Rack application, actually nothing but objects, response objects call method:

To run the above code, you just need to run the command rackup config.ru where config.ru is the file name to save the above code. Then go to the URL: http: // localhost: 9292 to view the application just written on.

When you enter the above URL, in the server running the above command will call and call method on Hello.new object

If you look at the call method, this method accepts an env hash containing information about the incoming HTTP request and the environment the application is running as argument, then returns an array of exactly 3 values: status, headers. and body

Now, we will learn a few things about Rack:

  • run: At run time, it receives a Ruby object, which will run the method call on it
  • map: It takes a string as a params and maps requests coming back to it, then if they match, it will run the code to handle them.
  • Use: They include middleware in the Rack application

What is Rack Middleware?


You can think of middleware as a small component to help perform a certain task. For example, as different intermediaries perform different processes like login, storage, etc.

It is possible to modify or pause requests before reaching the final middleware.

Middleware Stack

When you include more than one middleware, it will call the middleware stack. It is similar to the pipeline design pattern for a web server using Rack. That means that, for every incoming request that needs to pass through each middleware in the stack, the main order is sorted one by one as soon as they’re defined.

Built-in middlewares

Currently, there are many middleware already integrated in Rack applications, details you can refer to the link: https://github.com/rack/rack/wiki/List-of-Middleware

Creating custom Middleware and using it in Rack Application

We will create a custom middleware Cookie, which will add cookies to the response

First, start from the constructor method, and notice the app params in it. The app here is the next middleware and each middleware should include it in the constructor.

This middleware calls @ app.call (env) first, which will call the next middleware on the stack, in our case it’s Hello and its return response. Cookie middleware will then add the cookie to the response and return it in the response returned by the application

As explained above, the command use is used to add middleware to the stack, we can use Cookies middleware in our Rack application as above.

Rails and Rack

In a project using Rails, if you run: rake middleware, it will list the middleware being used in rails as follows:

For each request sent to your app, it will have to pass through the above middleware in this stack from top to bottom and finally it will be redirected to the routes, the route will dispatch the request to the controller, the controller handling them and return. Back response. This response will have to pass in turn through the above middleware in the stack respectively from bottom to top.

So where is the place in Rails that defines and runs them? Every Ruby on Rails project has a file named config.ru in the project’s root directory and it usually contains the following content:

This config.ru file loads the config / environment.rb file and runs the Rails.application command. This command will call Application defined in config / application.rb.

Now we will see what application.rb file has?

Here, the application inherits from Rails :: Application, Rails :: Application is inherited from Engine. This engine is a Rack app, it calls the method call defined in it and it will be run when config.ru calls Rails.application

How can I use it?

Rails allows us to configure the middleware stack as add, delete, change any middleware position in the stack.



Share the news now

Source : Viblo