Skip to content

Commit

Permalink
Add a default health controller (#46936)
Browse files Browse the repository at this point in the history
* 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.

* Add CHANGELOG entry

* Add test for HealthController

* Account for new default route
  • Loading branch information
dhh committed Jan 10, 2023
1 parent 86f33f3 commit 5c830a8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
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
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

0 comments on commit 5c830a8

Please sign in to comment.