lib/roda/plugins/middleware.rb in roda-2.13.0 vs lib/roda/plugins/middleware.rb in roda-2.14.0
- old
+ new
@@ -34,10 +34,20 @@
# run App
#
# It is possible to use the Roda app as a regular app even when using
# the middleware plugin.
module Middleware
+ # Configure the middleware plugin. Options:
+ # :env_var :: Set the environment variable to use to indicate to the roda
+ # application that the current request is a middleware request.
+ # You should only need to override this if you are using multiple
+ # roda middleware in the same application.
+ def self.configure(app, opts={})
+ app.opts[:middleware_env_var] = opts[:env_var] if opts.has_key?(:env_var)
+ app.opts[:middleware_env_var] ||= 'roda.forward_next'
+ end
+
# Forward instances are what is actually used as middleware.
class Forwarder
# Store the current middleware and the next middleware to call.
def initialize(mid, app)
@mid = mid
@@ -49,13 +59,12 @@
# pass handling of the request to the next middleware.
def call(env)
res = nil
call_next = catch(:next) do
- scope = @mid.new(env)
- scope.request.forward_next = true
- res = scope.call(&@mid.route_block)
+ env[@mid.opts[:middleware_env_var]] = true
+ res = @mid.call(env)
false
end
if call_next
@app.call(env)
@@ -87,10 +96,12 @@
end
module RequestMethods
# Whether to forward the request to the next application. Set only if
# this request is being performed for middleware.
- attr_accessor :forward_next
+ def forward_next
+ env[roda_class.opts[:middleware_env_var]]
+ end
end
end
register_plugin(:middleware, Middleware)
end