385 Rack and Middlewares

Updated . Posted . Visible to the public. Deprecated.

Streamlining the curriculum.

Goal of this lesson is to understand what middlewares in Rack are good for.

Rack

Start with these articles:

You should be able to answer the following questions:

  • What is Rack?
  • How does Rack relate to Ruby on Rails?
  • What is Rack middleware?
  • What are some advantages of implementing functionality as a middleware as opposed to e.g. a before_filter in ApplicationController?

Rails middleware

Now look at the middleware stack of a freshly installed Rails application Show archive.org snapshot . For each middleware in the stack:

  • What does it do?
  • What would happen if we removed it from the stack?
  • Can you find the code that implements it?

Exception Notification

We are using the exception_notification gem to receive an e-mail whenever one of our apps raises an unhandled exception.

Look through the repo Show archive.org snapshot and understand how it does this (hint: it’s a middleware!).

Excercises

  • Write a Middleware that makes a Rails application respond to GET /hello when added to the middleware stack. The path would simply print HI WORLD with a text/plain content type.
  • Write a Middleware that prints OUCH to the screen whenever the Rails application raises an unhandled error.
Henning Koch
Last edit
Jonas Schiele
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2015-08-04 18:36)