Class: Rage::Configuration::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/configuration.rb

Instance Method Summary collapse

Instance Method Details

#include?(middleware) ⇒ Boolean

Check if a middleware is included in the stack.

Parameters:

  • middleware (Class, Integer)

    the middleware class or its index in the stack

Returns:

  • (Boolean)


443
444
445
# File 'lib/rage/configuration.rb', line 443

def include?(middleware)
  !!find_middleware_index(middleware) rescue false
end

#insert_after(existing_middleware, new_middleware, *args, &block) ⇒ Object

Insert a new middleware after an existing middleware in the stack.

Examples:

Rage.configure do
  config.middleware.insert_after Rack::Runtime, Rack::Cors do
    allow do
      origins "*"
      resource "*", headers: :any
    end
  end
end

Parameters:

  • existing_middleware (Class, Integer)

    the existing middleware class or its index in the stack

  • new_middleware (Class)

    the new middleware class

  • args (Array)

    arguments passed to the middleware initializer

  • block (Proc)

    an optional block passed to the middleware initializer



435
436
437
438
# File 'lib/rage/configuration.rb', line 435

def insert_after(existing_middleware, new_middleware, *args, &block)
  index = find_middleware_index(existing_middleware)
  @middlewares = (@middlewares[0..index] + [[new_middleware, args, block]] + @middlewares[index + 1..]).uniq(&:first)
end

#insert_before(existing_middleware, new_middleware, *args, &block) ⇒ Object

Note:

Rage always uses the Rage::FiberWrapper middleware, which wraps every request in a separate fiber. Make sure to always have this middleware in the top of the stack. Placing other middlewares in front may lead to undefined behavior.

Insert a new middleware before an existing middleware in the stack.

Examples:

Rage.configure do
  config.middleware.insert_before Rack::Runtime, Rack::Cors do
    allow do
      origins "*"
      resource "*", headers: :any
    end
  end
end

Parameters:

  • existing_middleware (Class, Integer)

    the existing middleware class or its index in the stack

  • new_middleware (Class)

    the new middleware class

  • args (Array)

    arguments passed to the middleware initializer

  • block (Proc)

    an optional block passed to the middleware initializer



413
414
415
416
417
418
419
# File 'lib/rage/configuration.rb', line 413

def insert_before(existing_middleware, new_middleware, *args, &block)
  index = find_middleware_index(existing_middleware)
  if index == 0 && @middlewares[0][0] == Rage::FiberWrapper
    puts("Warning: inserting #{new_middleware} before Rage::FiberWrapper may lead to undefined behavior.")
  end
  @middlewares = (@middlewares[0...index] + [[new_middleware, args, block]] + @middlewares[index..]).uniq(&:first)
end

#use(new_middleware, *args, &block) ⇒ Object

Note:

This is the recommended way of adding a middleware.

Add a new middleware to the end of the stack.

Examples:

Rage.configure do
  config.middleware.use Rack::Cors do
    allow do
      origins "*"
      resource "*", headers: :any
    end
  end
end

Parameters:

  • new_middleware (Class)

    the middleware class

  • args (Array)

    arguments passed to the middleware initializer

  • block (Proc)

    an optional block passed to the middleware initializer



394
395
396
# File 'lib/rage/configuration.rb', line 394

def use(new_middleware, *args, &block)
  insert_after(@middlewares.length - 1, new_middleware, *args, &block)
end