Class: OpsRoutes::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) {|OpsRoutes| ... } ⇒ Middleware

Returns a new instance of Middleware.

Yields:



5
6
7
8
# File 'lib/ops_routes/middleware.rb', line 5

def initialize(app)
  @app = app
  yield OpsRoutes if block_given?
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ops_routes/middleware.rb', line 10

def call(env)
  return @app.call(env) unless env['PATH_INFO'] =~ %r{^/ops/(heartbeat(?:/(\w+))?|version|configuration)/?$}
  route, heartbeat_name = $1, $2
  case route
  when /^heartbeat/
    heartbeat_result = OpsRoutes.check_heartbeat(heartbeat_name)
    headers = { 'Content-Type' => 'text/plain',
                'Content-Length' => heartbeat_result[:text].length.to_s }
    [ heartbeat_result[:status], headers, [heartbeat_result[:text]] ]
  when 'version'
    version_result = OpsRoutes.check_version(env)
    headers = { 'Content-Type' => 'text/html',
                'Content-Length' => version_result.length.to_s }
    [ 200, headers, [version_result] ]
  when 'configuration'
    configuration_result = OpsRoutes.check_configuration
    headers = { 'Content-Type' => 'text/html',
                'Content-Length' => configuration_result.length.to_s }
    [ 200, headers, [configuration_result] ]
  end
end