Sha256: ae613d9a47a3d3c9b29d386cf12cddeee38148dd26dd32755d7f4a86497f3bf4

Contents?: true

Size: 1.57 KB

Versions: 40

Compression:

Stored size: 1.57 KB

Contents

# All roads lead here
#
# 1. AWS Lambda: PostsController - Rack::Adapter - Jets.application.call
# 2. Local server:  config.ru - run Jet.application - Jets.application.call
#
# Then eventually:
#
#   Jets.application.call - Middleware stack - Jets::Controller::Middleware::Main
#
module Jets::Controller::Middleware
  class Main
    def initialize(env)
      @env = env
      @controller = env['jets.controller']
      @event = env['lambda.event']
      @context = env['lambda.context']
      @meth = env['lambda.meth']
    end

    def call
      ENV['JETS_HOST'] = jets_host # Dirty to use what's essentially a global variable but unsure how else to achieve
      dup.call!
    end

    def call!
      setup
      @controller.dispatch! # Returns triplet
    end

    # Common setup logical at this point of middleware processing right before
    # calling any controller actions.
    def setup
      # We already recreated a mimic rack env earlier as part of the very first
      # middleware layer. However, by the time the rack env reaches the main middleware
      # it could had been updated by other middlewares. We update the env here again.
      @controller.request.set_env!(@env)
      # This allows sesison helpers to work. Sessions are managed by
      # the Rack::Session::Cookie middleware by default.
      @controller.session = @env['rack.session'] || {}
    end

    def jets_host
      default = "#{@env['rack.url_scheme']}://#{@env['HTTP_HOST']}"
      Jets.config.helpers.host || default
    end

    def self.call(env)
      instance = new(env)
      instance.call
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
jets-2.3.19 lib/jets/controller/middleware/main.rb
jets-2.3.18 lib/jets/controller/middleware/main.rb
jets-2.3.17 lib/jets/controller/middleware/main.rb
jets-2.3.16 lib/jets/controller/middleware/main.rb
jets-2.3.15 lib/jets/controller/middleware/main.rb
jets-2.3.14 lib/jets/controller/middleware/main.rb
jets-2.3.13 lib/jets/controller/middleware/main.rb
jets-2.3.12 lib/jets/controller/middleware/main.rb
jets-2.3.11 lib/jets/controller/middleware/main.rb
jets-2.3.10 lib/jets/controller/middleware/main.rb
jets-2.3.9 lib/jets/controller/middleware/main.rb
jets-2.3.8 lib/jets/controller/middleware/main.rb
jets-2.3.7 lib/jets/controller/middleware/main.rb
jets-2.3.6 lib/jets/controller/middleware/main.rb
jets-2.3.5 lib/jets/controller/middleware/main.rb
jets-2.3.4 lib/jets/controller/middleware/main.rb
jets-2.3.3 lib/jets/controller/middleware/main.rb
jets-2.3.2 lib/jets/controller/middleware/main.rb
jets-2.3.1 lib/jets/controller/middleware/main.rb
jets-2.3.0 lib/jets/controller/middleware/main.rb