Class: HttpHealthCheck::RackApp

Inherits:
Object
  • Object
show all
Defined in:
lib/http_health_check/rack_app.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(routes, fallback_app: DEFAULT_FALLBACK_APP, logger: nil) ⇒ RackApp

Returns a new instance of RackApp.



20
21
22
23
24
25
26
# File 'lib/http_health_check/rack_app.rb', line 20

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

Instance Attribute Details

#fallback_appObject (readonly)

Returns the value of attribute fallback_app.



27
28
29
# File 'lib/http_health_check/rack_app.rb', line 27

def fallback_app
  @fallback_app
end

#loggerObject (readonly)

Returns the value of attribute logger.



27
28
29
# File 'lib/http_health_check/rack_app.rb', line 27

def logger
  @logger
end

#routesObject (readonly)

Returns the value of attribute routes.



27
28
29
# File 'lib/http_health_check/rack_app.rb', line 27

def routes
  @routes
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:

  • (config)


12
13
14
15
16
17
18
# File 'lib/http_health_check/rack_app.rb', line 12

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

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
# File 'lib/http_health_check/rack_app.rb', line 29

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