Rack is a Ruby package which provides an interface for a web server to communicate with the application. It is very easy to add middleware components between the web server and the app to customize the way your request/response behaves. The middleware component sits between the client and the server, processing inbound requests and outbound responses. Rack Middleware is an implementation of the pipeline design pattern for web servers using Rack.
For example with Rack, we can have separate stages of the pipeline:
- Authentication: Checks whether the login details are correct or not when the request arrives.
- Authorization: It performs role-based security. i.e. checks whether the user is authorized to perform the particular task.
- Caching: Return a cached result if the request is already processed.
- Decoration: Enhance the request to make downstream processing better.
- Performance & Usage Monitoring: Status get from the request and response.
- Execution: actually handle the request and provide a response.
Next, we will see how to build our own rack middleware.