Sha256: 1cd2f3f9333f057eaa19511398e661a823836ca9bb727bc75931f77180fcb612

Contents?: true

Size: 1.67 KB

Versions: 10

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Hanami
  # @api private
  module Providers
    # Provider source to register Rack integration components in Hanami slices.
    #
    # @see Hanami::Providers::Logger
    # @see Hanami::Web::RackLogger
    # @see https://github.com/rack/rack
    # @see https://dry-rb.org/gems/dry-monitor/
    #
    # @api private
    # @since 2.0.0
    class Rack < Dry::System::Provider::Source
      # @api private
      def prepare
        Dry::Monitor.load_extensions(:rack)

        # Explicitly register the Rack middleware events on our notifications bus. The Dry::Monitor
        # rack extension (activated above) does register these globally, but if the notifications
        # bus has been used before this provider loads, then it will have created its own separate
        # locally copy of all registered events as of that moment in time, which will not included
        # the Rack events globally reigstered above.
        notifications = target["notifications"]
        notifications.register_event(Dry::Monitor::Rack::Middleware::REQUEST_START)
        notifications.register_event(Dry::Monitor::Rack::Middleware::REQUEST_STOP)
        notifications.register_event(Dry::Monitor::Rack::Middleware::REQUEST_ERROR)
      end

      # @api private
      def start
        target.start :logger

        monitor_middleware = Dry::Monitor::Rack::Middleware.new(
          target["notifications"],
          clock: Dry::Monitor::Clock.new(unit: :microsecond)
        )

        rack_logger = Hanami::Web::RackLogger.new(target[:logger], env: target.env)
        rack_logger.attach(monitor_middleware)

        register "monitor", monitor_middleware
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
hanami-2.1.0 lib/hanami/providers/rack.rb
hanami-2.1.0.rc3 lib/hanami/providers/rack.rb
hanami-2.1.0.rc2 lib/hanami/providers/rack.rb
hanami-2.1.0.rc1 lib/hanami/providers/rack.rb
hanami-2.1.0.beta2.1 lib/hanami/providers/rack.rb
hanami-2.1.0.beta2 lib/hanami/providers/rack.rb
hanami-2.1.0.beta1 lib/hanami/providers/rack.rb
hanami-2.0.3 lib/hanami/providers/rack.rb
hanami-2.0.2 lib/hanami/providers/rack.rb
hanami-2.0.1 lib/hanami/providers/rack.rb