Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Rack middleware?

What is Rack middleware in Ruby? I couldn't find any good explanation for what they mean by "middleware".

like image 514
chrisgoyal Avatar asked Oct 18 '22 12:10

chrisgoyal


People also ask

What are middleware in rails?

Middleware. You can refer rack middleware as a small component that assists with the execution of a task. For example, You can think of different middleware doing different processes like logging, caching. It can modify or halt requests before they reach the final middleware(The object which we assigned to run method).

What is a Rack application?

Rack is a modular interface between web servers and web applications developed in the Ruby programming language. With Rack, application programming interfaces (APIs) for web frameworks and middleware are wrapped into a single method call handling HTTP requests and responses.

How does a Rack work?

Rack provides a minimal, modular, and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.

What does Rack gem do?

Rack is a gem to interface your framework to a Ruby application server such as Mongrel, Thin, Lighttpd, Passenger, WEBrick or Unicorn. An application server is a special type of web server that runs server applications, often in Ruby.


1 Answers

Rack as Design

Rack middleware is more than "a way to filter a request and response" - it's an implementation of the pipeline design pattern for web servers using Rack.

It very cleanly separates out the different stages of processing a request - separation of concerns being a key goal of all well designed software products.

For example with Rack I can have separate stages of the pipeline doing:

  • Authentication: when the request arrives, are the users logon details correct? How do I validate this OAuth, HTTP Basic Authentication, name/password?

  • Authorization: "is the user authorised to perform this particular task?", i.e. role-based security.

  • Caching: have I processed this request already, can I return a cached result?

  • Decoration: how can I enhance the request to make downstream processing better?

  • Performance & Usage Monitoring: what stats can I get from the request and response?

  • Execution: actually handle the request and provide a response.

Being able to separate the different stages (and optionally include them) is a great help in developing well structured applications.

Community

There's also a great eco-system developing around Rack Middleware - you should be able to find pre-built rack components to do all of the steps above and more. See the Rack GitHub wiki for a list of middleware.

What's Middleware?

Middleware is a dreadful term which refers to any software component/library which assists with but is not directly involved in the execution of some task. Very common examples are logging, authentication and the other common, horizontal processing components. These tend to be the things that everyone needs across multiple applications but not too many people are interested (or should be) in building themselves.

More Information

  • The comment about it being a way to filter requests probably comes from the RailsCast episode 151: Rack Middleware screen cast.

  • Rack middleware evolved out of Rack and there is a great intro at Introduction to Rack middleware.

  • There's an intro to middleware on Wikipedia here.

like image 379
Chris McCauley Avatar answered Oct 24 '22 02:10

Chris McCauley