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 all commits
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
6 changes: 6 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Add Rails::HealthController#show and map it to /up for newly generated applications.
Load balancers and uptime monitors all need a basic endpoint to tell whether the app is up.
This is a good starting point that'll work in many situations.

*DHH*

* Only use HostAuthorization middleware if `config.hosts` is not empty

*Hartley McGuire*
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Rails
extend ActiveSupport::Autoload
extend ActiveSupport::Benchmarkable

autoload :HealthController
autoload :Info
autoload :InfoController
autoload :MailersController
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions railties/test/application/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ def index
assert_equal 404, last_response.status
end

test "rails/health in production" do
app("production")

app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
get "up" => "rails/health#show"
end
RUBY

get "/up"
assert_equal 200, last_response.status
end

test "simple controller" do
simple_controller

Expand Down
3 changes: 2 additions & 1 deletion railties/test/generators/scaffold_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,9 @@ def test_scaffold_generator_on_revoke_does_not_mutilate_routes
route_path = File.expand_path("config/routes.rb", destination_root)
content = File.read(route_path)

# Remove all of the comments and blank lines from the routes file
# Remove all of the comments, blank lines, and default health controller from the routes file
content.gsub!(/^ \#.*\n/, "")
content.gsub!(/^ get "up".*\n/, "")
content.gsub!(/^\n/, "")

File.write(route_path, content)
Expand Down