Sha256: 9689d6695b317a8bf9d2079eb3d273d79d42d6aa0a2eb8a18ce9ce791fe50547

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require 'json'
require 'logger'

module HttpHealthCheck
  class RackApp
    HEADERS = { 'Content-Type' => 'application/json' }.freeze
    DEFAULT_FALLBACK_APP = ->(_env) { [404, HEADERS, ['{"error": "not_found"}']] }.freeze
    LIVENESS_CHECK_APP = ->(_env) { [200, HEADERS, ["{}\n"]] }

    def self.configure
      config = Config::Dsl.new
      yield config

      fallback_app = config.configured_fallback_app || DEFAULT_FALLBACK_APP
      new(config.routes, fallback_app: fallback_app, logger: config.configured_logger)
    end

    def initialize(routes, fallback_app: DEFAULT_FALLBACK_APP, logger: nil)
      @logger = logger || Logger.new(IO::NULL, level: Logger::Severity::UNKNOWN)
      @fallback_app = ensure_callable!(fallback_app)
      @routes = routes.each_with_object('/liveness' => LIVENESS_CHECK_APP) do |(path, handler), acc|
        acc[path.to_s] = ensure_callable!(handler)
      end
    end
    attr_reader :routes, :fallback_app, :logger

    def call(env)
      result = routes.fetch(env[Rack::REQUEST_PATH], fallback_app).call(env)
      return result unless result.is_a?(Probe::Result)

      [result.ok? ? 200 : 500, HEADERS, [result.meta.to_json]]
    end

    private

    def ensure_callable!(obj)
      return obj if obj.respond_to?(:call)

      raise ::HttpHealthCheck::ConfigurationError, 'HTTP handler must be callable'
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
http_health_check-0.5.0 lib/http_health_check/rack_app.rb
http_health_check-0.4.1 lib/http_health_check/rack_app.rb
http_health_check-0.4.0 lib/http_health_check/rack_app.rb
http_health_check-0.3.1 lib/http_health_check/rack_app.rb
http_health_check-0.3.0 lib/http_health_check/rack_app.rb
http_health_check-0.2.1 lib/http_health_check/rack_app.rb
http_health_check-0.2.0 lib/http_health_check/rack_app.rb