class Leafy::Rack::Health

Public Class Methods

new(app, registry, path = '/health') click to toggle source
# File leafy-rack/lib/leafy/rack/health.rb, line 28
def initialize(app, registry, path = '/health')
  @app = app
  @path = path
  @registry = registry
end
response( health, env ) click to toggle source
# File leafy-rack/lib/leafy/rack/health.rb, line 9
def self.response( health, env )
  data = health.run_health_checks
  is_healthy = data.values.all? { |r| r.healthy? }
  json = if env[ 'QUERY_STRING' ] == 'pretty'
           JSON.pretty_generate( data.to_hash )
         else
           # to use data.to_hash.to_json did produce
           # a different json structure on some
           # rack setup
           JSON.generate( data.to_hash )
         end
  [
   is_healthy ? 200 : 503, 
   { 'Content-Type' => 'application/json',
     'Cache-Control' => 'must-revalidate,no-cache,no-store' }, 
   [ json ]
  ]
end

Public Instance Methods

call(env) click to toggle source
# File leafy-rack/lib/leafy/rack/health.rb, line 34
def call(env)
  if env['PATH_INFO'] == @path
    Health.response( @registry.health, env )
  else
    @app.call( env )
  end
end