Sha256: b2f20904b4fbec037c966c4e74b9f974f948554560fbf449c4e3022ac0e37cdc

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

require 'rack'

module Hanami
  # Rack compatible server.
  #
  # It is run with:
  #
  #   `bundle exec hanami server`
  #
  # It runs the application, by using the server specified in your `Gemfile`
  # (eg. Puma or Unicorn).
  #
  # @since 0.8.0
  # @api private
  class Server < ::Rack::Server
    attr_reader :options

    # @since 0.8.0
    # @api private
    #
    # @see Hanami::Environment#initialize
    def initialize
      @options = _extract_options
      setup
    end

    # Primarily this removes the ::Rack::Chunked middleware
    # which is the cause of Safari content-length bugs.
    #
    # @since 0.8.0
    def middleware
      mw = Hash.new { |e, m| e[m] = [] }
      mw["deployment"].concat([::Rack::ContentLength, ::Rack::CommonLogger])
      mw["development"].concat(mw["deployment"] + [::Rack::ShowExceptions, ::Rack::Lint])
      require 'hanami/assets/static'
      mw["development"].push(::Hanami::Assets::Static)
      mw
    end

    def start
      preload
      super
    end

    private

    def setup
      if code_reloading?
        @app = Shotgun::Loader.new(rackup, &reloadable)
      else
        reloadable.call
      end
    end

    def reloadable
      ->(*) { Hanami::Components.resolve('model', 'finalizers') }
    end

    def environment
      Components['environment']
    end

    # @since 0.8.0
    # @api private
    def code_reloading?
      Components['code_reloading']
    end

    def rackup
      environment.rackup.to_s
    end

    def preload
      if code_reloading?
        Shotgun.enable_copy_on_write
        Shotgun.preload
      else
        Hanami.boot
      end
    end

    # Options for Rack::Server superclass
    #
    # @since 0.8.0
    # @api private
    def _extract_options
      environment.to_options.merge(
        config:      rackup,
        Host:        environment.host,
        Port:        environment.port,
        AccessLog:   []
      )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hanami-0.9.2 lib/hanami/server.rb
hanami-0.9.1 lib/hanami/server.rb
hanami-0.9.0 lib/hanami/server.rb