class Rack::ECG

Constants

DEFAULT_CHECKS

Checks enabled by default.

DEFAULT_MOUNT_AT

Default mount path.

VERSION

Library version.

Public Class Methods

new(app = nil, checks: DEFAULT_CHECKS, at: DEFAULT_MOUNT_AT, hook: nil) click to toggle source

Constructs an instance of ECG Rack middleware with the specified options.

@param app [Object,nil] Underlying Rack application to receive unmatched

requests. If unset, any unmatched requests will return a 404.

@param checks [Array<Symbol, Array<Symbol, Object>>] Sets and

configures the checks run by this instance.

@param at [String, nil] Path which this ECG instance handles. @param hook [#call, nil] Callable which receives the success status and

check results
# File lib/rack/ecg.rb, line 24
def initialize(app = nil, checks: DEFAULT_CHECKS, at: DEFAULT_MOUNT_AT, hook: nil)
  @app = app

  check_configuration = checks || []
  @check_factory = CheckFactory.new(check_configuration, DEFAULT_CHECKS)
  @mount_at = at || DEFAULT_MOUNT_AT

  @result_hook = hook
end

Public Instance Methods

call(env) click to toggle source

Rack compatible call method. Not intended for direct usage.

# File lib/rack/ecg.rb, line 35
def call(env)
  if env["PATH_INFO"] == @mount_at
    check_results = @check_factory.build_all.inject({}) do |results, check|
      results.merge(check.result.as_json)
    end

    success = check_results.none? { |check| check[1][:status] == Check::Status::ERROR }

    response_status = success ? 200 : 500

    @result_hook&.call(success, check_results)

    response_headers = {
      "X-Rack-ECG-Version" => Rack::ECG::VERSION,
      "Content-Type" => "application/json",
    }

    response_body = JSON.pretty_generate(check_results)

    [response_status, response_headers, [response_body]]
  elsif @app
    @app.call(env)
  else
    [404, {}, []]
  end
end