Rack

Njunu-sk
2 min readApr 10, 2021
rack middle-ware

What is Rack ?

Rack in the underlying technology behind nearly all the web frameworks in the Ruby world.

Rack is a layer between the framework and the application server.

Rack architecture defines a very simple interface and any code that conforms to this interface can be used in a Rack application.

Rack wraps HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks and software in between into a single method call.

Rack app is simply a Ruby object that responds to #call and accepts an environment hash(env), which contains all of the data about a request and returns an ordered array of HTTP status code, headers and body of the actual content of the response.

middle-ware

Note

Servers — these are crucial components of the request response process

Unicorn Architecture — unicorn is a preforking application server that conforms to the Rack interface.

Preforking means that it uses a forked worker process to concurrently handle requests.

The internals

When a server starts up, the master process loads the rails app code into memory, forks some number of workers, monitors them and traps incoming signals.

Workers are responsible for handling requests, they read incoming HTTP requests, sends them into the rails application and write back the received response.

The received response in a fully rendered layout and view.

Rack middle-ware is a way of filtering requests and responses and separates the stages of request processing into a pipeline.

How it works

When the request comes in it starts with the first piece of middle-ware in the list and then moves down going through each one until it finally reaches the application routes. When a request bubbles down to the routes we arrive at the routing stage where we need to match the request URL to a controller action and invoke it to get a response.

References

--

--