class RailsHealthCheck::Middleware

Public Class Methods

new(app, options={}) click to toggle source
# File lib/rails-health-check/middleware.rb, line 4
def initialize(app, options={})
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/rails-health-check/middleware.rb, line 8
def call(env)
  if env['REQUEST_METHOD'] == 'GET' && health_path?(env['PATH_INFO'])
    if health_latency_path?(env['PATH_INFO'])
      health_latency_response
    elsif health_db_path?(env['PATH_INFO'])
      health_db_response
    end
  else
    @app.call(env)
  end
end

Protected Instance Methods

health_db_path?(request_path) click to toggle source
# File lib/rails-health-check/middleware.rb, line 30
def health_db_path?(request_path)
  request_path == '/health/db'
end
health_db_response() click to toggle source
# File lib/rails-health-check/middleware.rb, line 39
def health_db_response
  begin
    validate_migration

    status = 200
    msg = 'ok'
  rescue => e
    status = 500
    msg = e.message
  end

  # [status, headers, body]
  [status, { 'Content-Type' => 'application/json' }, [{ msg: msg }.to_json]]
end
health_latency_path?(request_path) click to toggle source
# File lib/rails-health-check/middleware.rb, line 26
def health_latency_path?(request_path)
  request_path == '/health/latency'
end
health_latency_response() click to toggle source
# File lib/rails-health-check/middleware.rb, line 34
def health_latency_response
  # [status, headers, body]
  [200, { 'Content-Type' => 'application/json' }, [{ msg: 'ok' }.to_json]]
end
health_path?(request_path) click to toggle source
# File lib/rails-health-check/middleware.rb, line 22
def health_path?(request_path)
  health_latency_path?(request_path) || health_db_path?(request_path)
end
validate_migration() click to toggle source
# File lib/rails-health-check/middleware.rb, line 54
def validate_migration
  # Check connection to the DB and needs migration or not
  if ActiveRecord::Migrator.needs_migration?
    raise 'Schema is not latest!'
  end
end