-
Notifications
You must be signed in to change notification settings - Fork 21.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a default health controller (#46936)
* 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
Showing
6 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
railties/lib/rails/generators/rails/app/templates/config/routes.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters