Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save necojackarc/35bac9f8cdeed5cf828e22d318bdb5d3 to your computer and use it in GitHub Desktop.
Save necojackarc/35bac9f8cdeed5cf828e22d318bdb5d3 to your computer and use it in GitHub Desktop.
How to Control Maintenance Mode (Render 503) by Env Vars with Rails
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :render_503_except_for_whitelisted_ips, if: :maintenance_mode?
def maintenance_mode?
ENV["MAINTENANCE_MODE"] == "true"
end
def render_503_except_for_whitelisted_ips
ips_in_whitelist = (ENV["ALLOWED_IPS"] || "").split(",")
return if ips_in_whitelist.include?(request.remote_ip)
render(
file: Rails.public_path.join("503.html"),
content_type: "text/html",
layout: false,
status: :service_unavailable,
)
end
end

Environment variables

Key VALUE
MAINTENANCE_MODE When "true", your Rails app runs as maintenance mode
ALLOWED_IPS Set IP addresses you allow to access your Rails app when maintenance mode

Example

$ MAINTENANCE_MODE=true ALLOWED_IPS=192.168.145.2,192.168.145.3 bin/rails s -b 0.0.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment