Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a default health controller #46936

Merged
merged 4 commits into from Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
Add a default health controller
Load balancers and uptime monitors all need a basic endpoint to tell whether the app is up. Here's a good starting point that'll work in many situations.
  • Loading branch information
dhh committed Jan 9, 2023
commit 9e093d124136b9e3ed0a9184407fdc3afc5d5c3b
1 change: 1 addition & 0 deletions railties/lib/rails.rb
Expand Up @@ -28,6 +28,7 @@ module Rails
extend ActiveSupport::Autoload
extend ActiveSupport::Benchmarkable

autoload :HealthController
autoload :Info
autoload :InfoController
autoload :MailersController
Expand Down
@@ -1,6 +1,10 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show"

# Defines the root path route ("/")
# root "articles#index"
end
22 changes: 22 additions & 0 deletions railties/lib/rails/health_controller.rb
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class Rails::HealthController < ActionController::Base # :nodoc:
rescue_from(Exception) { render_down }

def show
render_up
end

private
def render_up
render html: html_status(color: "green")
end

def render_down
render html: html_status(color: "red"), status: 500
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't 503 Service Unavailable be a more appropriate HTTP code in this case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a PR to address this: #47061

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

end

def html_status(color:)
%(<html><body style="background-color: #{color}"></body></html>).html_safe
end
end