GithubHelp home page GithubHelp logo

Comments (1)

ndbroadbent avatar ndbroadbent commented on May 19, 2024

I figured out a way to do this! Here's my full config that implements some of the logic I was using in the health_check gem:

# config/initializers/health_bit.rb

def configure_healthbit(klass)
  klass.configure do |c|
    c.success_text = 'success'
    c.headers = {
      'Content-Type' => 'text/plain;charset=utf-8',
      'Cache-Control' => 'private,max-age=0,must-revalidate,no-store',
    }
    c.success_code = 200
    c.fail_code = 500
    c.show_backtrace = false
  end
end

HealthBit::FullCheck = HealthBit.clone
configure_healthbit(HealthBit::FullCheck)
configure_healthbit(HealthBit)

HealthBit::FullCheck.add('Database') do
  ApplicationRecord.connection.select_value('SELECT 1') == 1
end
HealthBit::FullCheck.add('Sidekiq') do
  Sidekiq.redis(&:ping) == 'PONG'
end
HealthBit::FullCheck.add('Rails cache') do
  Rails.cache.write('__health_bit__', '1', expires_in: 1.second)
end
# app/lib/rack/health_bit.rb

module Rack
  class HealthBit
    def initialize(app)
      @app = app
    end

    def call(env)
      case env['PATH_INFO']
      when '/health/site'
        return ::HealthBit.rack.call(env)
      when '/health', '/health/all'
        return ::HealthBit::FullCheck.rack.call(env)
      end
      @app.call(env)
    end
  end
end
# config/application.rb

# Load custom Rack middleware
Dir.glob(Rails.root.join('app', 'lib', 'rack', '*')).sort.each { |f| require f }
# ...

config.middleware.insert_before 0, Rack::HealthBit

(And no changes to config/routes.rb.)

This successfully responds to any request and bypasses the Rails hosts check.

Do you think it might be good to suggest this as the default configuration? I think it's generally important for apps to always respond to /health without any additional Rails middleware or checks.

from health_bit.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.