Class: Leafy::Rack::Health

Inherits:
Object
  • Object
show all
Defined in:
lib/leafy/rack/health.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, registry, path = '/health') ⇒ Health

Returns a new instance of Health.



28
29
30
31
32
# File 'lib/leafy/rack/health.rb', line 28

def initialize(app, registry, path = '/health')
  @app = app
  @path = path
  @registry = registry
end

Class Method Details

.response(health, env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File '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

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
40
# File '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