Class: Rage::Configuration::MiddlewareRegistry

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

Direct Known Subclasses

Deferred::Middleware, Middleware, Telemetry

Instance Method Summary collapse

Instance Method Details

#delete(middleware) ⇒ Object

Delete a middleware from the stack.

Examples:

Rage.configure do
  config.middleware.delete Rack::Cors
end

Parameters:

  • middleware (Class)

    the middleware class



470
471
472
# File 'lib/rage/configuration.rb', line 470

def delete(middleware)
  @objects.reject! { |o, _, _| o == middleware }
end

#include?(middleware) ⇒ Boolean

Check if a middleware is included in the stack.

Parameters:

  • middleware (Class)

    the middleware class

Returns:

  • (Boolean)


460
461
462
# File 'lib/rage/configuration.rb', line 460

def include?(middleware)
  @objects.any? { |o, _, _| o == middleware }
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



450
451
452
453
454
455
# File 'lib/rage/configuration.rb', line 450

def insert_after(existing_middleware, new_middleware, *args, &block)
  index = find_object_index(existing_middleware) + 1
  index = 0 if @objects.empty?
  validate!(index, new_middleware)
  @objects.insert(index, [new_middleware, args, block])
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



430
431
432
433
434
# File 'lib/rage/configuration.rb', line 430

def insert_before(existing_middleware, new_middleware, *args, &block)
  index = find_object_index(existing_middleware)
  validate!(index, new_middleware)
  @objects.insert(index, [new_middleware, args, block])
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



410
411
412
413
# File 'lib/rage/configuration.rb', line 410

def use(new_middleware, *args, &block)
  validate!(-1, new_middleware)
  @objects.insert(-1, [new_middleware, args, block])
end