Class: MessageBus::Rack::Diagnostics

Inherits:
Object
  • Object
show all
Defined in:
lib/message_bus/rack/diagnostics.rb

Overview

Accepts requests from clients interested in using diagnostics functionality

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(app, config = {}) ⇒ Diagnostics

Returns a new instance of Diagnostics.

Parameters:

  • app (Proc)

    the rack app

  • config (Hash) (defaults to: {})

Options Hash (config):

  • :message_bus (MessageBus::Instance) — default: `MessageBus`

    a specific instance of message_bus



11
12
13
14
# File 'lib/message_bus/rack/diagnostics.rb', line 11

def initialize(app, config = {})
  @app = app
  @bus = config[:message_bus] || MessageBus
end

Instance Method Details

#call(env) ⇒ Object

Process an HTTP request from a subscriber client

Parameters:

  • env (Rack::Request::Env)

    the request environment



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
# File 'lib/message_bus/rack/diagnostics.rb', line 18

def call(env)
  return @app.call(env) unless env['PATH_INFO'].start_with? "#{@bus.base_route}message-bus/_diagnostics"

  route = env['PATH_INFO'].split("#{@bus.base_route}message-bus/_diagnostics")[1]

  if @bus.is_admin_lookup.nil? || !@bus.is_admin_lookup.call(env)
    return [403, {}, ['not allowed']]
  end

  return index unless route

  if route == '/discover'
    user_id = @bus.user_id_lookup.call(env)
    @bus.publish('/_diagnostics/discover', user_id: user_id)
    return [200, {}, ['ok']]
  end

  if route =~ /^\/hup\//
    hostname, pid = route.split('/hup/')[1].split('/')
    @bus.publish('/_diagnostics/hup', hostname: hostname, pid: pid.to_i)
    return [200, {}, ['ok']]
  end

  asset = route.split('/assets/')[1]
  if asset && !asset !~ /\//
    content = asset_contents(asset)
    split = asset.split('.')
    return [200, { 'Content-Type' => 'application/javascript;charset=UTF-8' }, [content]]
  end

  [404, {}, ['not found']]
end