Sha256: 7b7cd32ceeb23698a271c1ab5df195f241c863954c47944461a4925a794292e1

Contents?: true

Size: 1.86 KB

Versions: 22

Compression:

Stored size: 1.86 KB

Contents

module Jets::Middleware
  class DefaultStack
    attr_reader :config, :app
    def initialize(app, config)
      @app = app
      @config = config
    end

    def build_stack
      Stack.new do |middleware|
        middleware.use Rack::Runtime
        middleware.use Jets::Controller::Middleware::Cors if cors_enabled?
        middleware.use Rack::MethodOverride # must come before Middleware::Local for multipart post forms to work
        middleware.use Jets::Controller::Middleware::Local # mimics AWS Lambda for local server only
        middleware.use session_store, session_options # use session_store, session_options
        middleware.use Rack::Head
        middleware.use Rack::ConditionalGet
        middleware.use Rack::ETag
        use_webpacker(middleware)
      end
    end

  private
    def cors_enabled?
      Jets.config.cors
    end

    # Written as method to easily not include webpacker for case when either
    # webpacker not installed at all or disabled upon `jets deploy`.
    def use_webpacker(middleware)
      return unless Jets.webpacker? # checks for local development if webpacker installed
      # Different check for middleware because we need webpacker helpers for url helpers.
      # But we dont want to actually serve via webpacker middleware when running on AWS.
      # By this time the url helpers are serving assets out of s3.
      return if File.exist?("#{Jets.root}/config/disable-webpacker-middleware.txt") # created as part of `jets deploy`
      require "jets/controller/middleware/webpacker_setup"
      middleware.use Webpacker::DevServerProxy
    end

    def session_store
      Jets.config.session[:store] # do not use dot notation. session.store is a method on ActiveSupport::OrderedOptions.new
    end

    def session_options
      defaults = { secret: ENV['SECRET_KEY_BASE'] }
      defaults.merge(Jets.config.session.options)
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
jets-1.9.1 lib/jets/middleware/default_stack.rb
jets-1.9.0 lib/jets/middleware/default_stack.rb
jets-1.8.14 lib/jets/middleware/default_stack.rb
jets-1.8.13 lib/jets/middleware/default_stack.rb
jets-1.8.12 lib/jets/middleware/default_stack.rb
jets-1.8.11 lib/jets/middleware/default_stack.rb
jets-1.8.10 lib/jets/middleware/default_stack.rb
jets-1.8.9 lib/jets/middleware/default_stack.rb
jets-1.8.8 lib/jets/middleware/default_stack.rb
jets-1.8.7 lib/jets/middleware/default_stack.rb
jets-1.8.6 lib/jets/middleware/default_stack.rb
jets-1.8.5 lib/jets/middleware/default_stack.rb
jets-1.8.4 lib/jets/middleware/default_stack.rb
jets-1.8.3 lib/jets/middleware/default_stack.rb
jets-1.8.2 lib/jets/middleware/default_stack.rb
jets-1.8.1 lib/jets/middleware/default_stack.rb
jets-1.8.0 lib/jets/middleware/default_stack.rb
jets-1.7.2 lib/jets/middleware/default_stack.rb
jets-1.7.1 lib/jets/middleware/default_stack.rb
jets-1.7.0 lib/jets/middleware/default_stack.rb