Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.

Hydra Middleware Example

cam156 edited this page Jun 11, 2012 · 2 revisions

Rack middleware is a way to filter a request and response coming into your application. This can beuseful to see what request are completeing or not completing within a timely manner.

Here is a presentation about middleware from rack to give you an idea about what it can beused for: http://railscasts.com/episodes/151-rack-middleware

The example below was adapted from here: http://rubyforge.org/pipermail/mongrel-unicorn/2011-December.txt

To run Hydra with middleware you need to complete two steps:

  1. Create a middleware class
  2. Register the class as middleware

1) Create a Middleware class and locate it within config/initializers The one below would be located in config/initializers/log_before_timeout.rb

class LogBeforeTimeout < Struct.new(:app)
 
  def call(env)
    path = env["PATH_INFO"]
    qs = env["QUERY_STRING"] and path = "#{path}?#{qs}"
    env["rack.logger"].warn "START #{path}"
    app.call(env)
  ensure
    env["rack.logger"].warn "FINISH #{path}"
  end
end

  1. Register the middleware in config/application.rb Add a line to the application.rb to define your middleware class for use.
 module ScholarSphere
   class Application < Rails::Application
    config.middleware.use 'LogBeforeTimeout'

You can test your registration by running: rake middleware

Clone this wiki locally