Class: RailsHealthChecks::Rack::App

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_health_checks/rack/app.rb

Overview

Mountable Rack app exposing the same endpoints as the Rails engine. Mount in config.ru:

require "rails_health_checks/rack/app"
map "/health" { run RailsHealthChecks::Rack::App }

Or in Sinatra/Roda via their mount helpers.

Available routes (relative to mount point):

GET/HEAD /          → JSON health (same shape as Rails engine)
GET/HEAD /live      → plain-text liveness probe
GET      /metrics   → Prometheus text format
GET      /:group    → scoped group JSON

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ App

Returns a new instance of App.



27
28
29
30
# File 'lib/rails_health_checks/rack/app.rb', line 27

def initialize(env)
  @env = env
  @request = ::Rack::Request.new(env)
end

Class Method Details

.call(env) ⇒ Object



23
24
25
# File 'lib/rails_health_checks/rack/app.rb', line 23

def self.call(env)
  new(env).call
end

Instance Method Details

#callObject



32
33
34
35
36
37
# File 'lib/rails_health_checks/rack/app.rb', line 32

def call
  return unauthorized unless authorized?

  response = dispatch
  @request.head? ? [response[0], response[1], []] : response
end

#dispatchObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rails_health_checks/rack/app.rb', line 39

def dispatch
  path   = @request.path_info.delete_suffix("/")
  method = @request.request_method

  case [method, path]
  when ["GET", ""], ["HEAD", ""]
    health_response
  when ["GET", "/live"], ["HEAD", "/live"]
    live_response
  when ["GET", "/metrics"]
    metrics_response
  else
    if %w[GET HEAD].include?(method) && (m = path.match(%r{\A/([^/]+)\z}))
      group_response(m[1])
    else
      not_found
    end
  end
end