class MicroCommon::MetaMiddleware

Public Class Methods

new(app) click to toggle source
# File lib/micro_common/meta_middleware.rb, line 3
def initialize(app)
  @app = app
end

Public Instance Methods

about() click to toggle source
# File lib/micro_common/meta_middleware.rb, line 21
def about
  response = JSON.pretty_generate(YAML.safe_load(File.read(Rails.root.join('config', 'about.yaml'))))
  [200, { 'Content-Type' => 'application/json' }, [response]]
end
call(env) click to toggle source
# File lib/micro_common/meta_middleware.rb, line 7
def call(env)
  uri = env['PATH_INFO']

  if uri == '/meta/about'
    about
  elsif uri == '/meta/ping'
    ping
  elsif uri == '/meta/status'
    status
  else
    @app.call(env)
  end
end
check_database() click to toggle source
# File lib/micro_common/meta_middleware.rb, line 42
def check_database
  ActiveRecord::Base.connection.execute('SELECT 1')
  true
rescue PG::Error, PG::ConnectionBad, ActiveRecord::NoDatabaseError
  false
end
ping() click to toggle source
# File lib/micro_common/meta_middleware.rb, line 26
def ping
  [200, {}, ['PONG']]
end
run_checks() click to toggle source
# File lib/micro_common/meta_middleware.rb, line 36
def run_checks
  {
    database: check_database
  }
end
status() click to toggle source
# File lib/micro_common/meta_middleware.rb, line 30
def status
  checks = run_checks
  status = checks.all? { |_, v| v } ? 200 : 503
  [status, { 'Content-Type' => 'application/json' }, [checks.to_json]]
end