Module: Jets::Middleware

Extended by:
Memoist
Included in:
Application
Defined in:
lib/jets/middleware/configurator.rb,
lib/jets/middleware.rb,
lib/jets/middleware/layer.rb,
lib/jets/middleware/stack.rb,
lib/jets/middleware/default_stack.rb

Overview

Based on Rails MiddlewareStackProxy

Configurator is a recorder for the Jets middleware stack that allows you to configure middlewares in your application. It works basically as a command recorder, saving each command to be applied after initialization over the default middleware stack, so you can add, swap, or remove any middleware in Jets.

You can add your own middlewares by using the config.middleware.use method:

config.middleware.use Magical::Unicorns

This will put the Magical::Unicorns middleware on the end of the stack. You can use insert_before if you wish to add a middleware before another:

config.middleware.insert_before Rack::Head, Magical::Unicorns

There’s also insert_after which will insert a middleware after another:

config.middleware.insert_after Rack::Head, Magical::Unicorns

Middlewares can also be completely swapped out and replaced with others:

config.middleware.swap ActionDispatch::Flash, Magical::Unicorns

And finally they can also be removed from the stack completely:

config.middleware.delete ActionDispatch::Flash

Defined Under Namespace

Classes: Configurator, DefaultStack, Layer, Stack

Instance Method Summary collapse

Instance Method Details

#build_stackObject

Called in Jets::Booter to build middleware stack only once during bootup



16
17
18
# File 'lib/jets/middleware.rb', line 16

def build_stack
  middlewares
end

#call(env) ⇒ Object



5
6
7
8
# File 'lib/jets/middleware.rb', line 5

def call(env)
  stack = middlewares.build(endpoint)
  stack.call(env)
end

#config_middlewareObject



29
30
31
# File 'lib/jets/middleware.rb', line 29

def config_middleware
  Jets.config.middleware # returns Jets::Middleware::Configurator
end

#default_stackObject



25
26
27
# File 'lib/jets/middleware.rb', line 25

def default_stack
  Jets::Middleware::DefaultStack.new(Jets.config, Jets.application).build_stack # returns Jets::Middleware::Stack
end

#endpointObject

Final middleware in the stack



11
12
13
# File 'lib/jets/middleware.rb', line 11

def endpoint
  Jets::Controller::Middleware::Main
end

#middlewaresObject



20
21
22
# File 'lib/jets/middleware.rb', line 20

def middlewares
  config_middleware.merge_into(default_stack) # returns Jets::Middleware::Stack
end