Class: AliveState::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/alive_state/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
# File 'lib/alive_state/middleware.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#alive_state_call(env, format) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/alive_state/middleware.rb', line 17

def alive_state_call(env, format)
  format ||= AliveState.config.format

  status = 200
  params = { 'Content-Type' => 'text/plain; charset=utf-8' }
  raw = {
    state: :green,
    observed_at: Time.current.to_s,
    version: AliveState::Info.version_format,
    application: {}
  }

  AliveState.applications.each do |application|
    raw[:application][application.name] = application.alive?
  end

  raw[:application].keys.each do |key|
    unless raw[:application][key]
      raw[:state] = :red
      break
    end
  end

  case format
  when 'json'
    body = raw.to_json
    params = { 'Content-Type' => 'application/json' }
  when 'xml'
    body = raw.to_xml
    params = { 'Content-Type' => 'application/xml' }
  else
    body = raw.to_s
  end

  [status, params, [body]]
end

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/alive_state/middleware.rb', line 7

def call(env)
  path, format = env['PATH_INFO'].split('.')
  case path
  when /^\/#{AliveState.config.path}/
    alive_state_call(env, format)
  else
    @app.call(env)
  end
end