Class: Rack::ECG

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/ecg.rb,
lib/rack/ecg/check.rb,
lib/rack/ecg/version.rb,
lib/rack/ecg/check/http.rb,
lib/rack/ecg/check/error.rb,
lib/rack/ecg/check/static.rb,
lib/rack/ecg/check_factory.rb,
lib/rack/ecg/check_registry.rb,
lib/rack/ecg/check/git_revision.rb,
lib/rack/ecg/check/redis_connection.rb,
lib/rack/ecg/check/migration_version.rb,
lib/rack/ecg/check/sequel_connection.rb,
lib/rack/ecg/check/active_record_connection.rb

Defined Under Namespace

Modules: Check Classes: CheckFactory, CheckRegistry

Constant Summary collapse

DEFAULT_MOUNT_AT =

Default mount path.

"/_ecg"
DEFAULT_CHECKS =

Checks enabled by default.

[:http]
DEFAULT_FAILURE_STATUS =

Default failure response status.

500
VERSION =

Library version.

"0.3.0"

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, checks: DEFAULT_CHECKS, at: DEFAULT_MOUNT_AT, hook: nil, failure_status: DEFAULT_FAILURE_STATUS) ⇒ ECG

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



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rack/ecg.rb', line 28

def initialize(app = nil, checks: DEFAULT_CHECKS, at: DEFAULT_MOUNT_AT, hook: nil,
  failure_status: DEFAULT_FAILURE_STATUS)
  @app = app

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

  @result_hook = hook

  @failure_response_status = failure_status
end

Instance Method Details

#call(env) ⇒ Object

Rack compatible call method. Not intended for direct usage.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rack/ecg.rb', line 42

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 : @failure_response_status

    @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