Skip to content

Instantly share code, notes, and snippets.

@nkryptic
Created March 30, 2009 22:59
Show Gist options
  • Save nkryptic/87935 to your computer and use it in GitHub Desktop.
Save nkryptic/87935 to your computer and use it in GitHub Desktop.
# Allow the metal piece to run in isolation
require( File.dirname(__FILE__) + "/../../config/environment" ) unless defined?(Rails)
class PassengerRestarter
@restart_url = "/restart"
@default_target = "/"
@restart_message = "Restarted."
@set_flash = true
def self.call(env)
if Rails.env == 'development' && env["PATH_INFO"] =~ /^#{Regexp.quote(@restart_url)}/
perform_restart
set_flash_message(env) if @set_flash
redirect( redirect_target(env) )
else
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
end
end
def self.perform_restart
File.new( Rails.root.join("tmp/restart.txt"), "w" )
end
def self.set_flash_message(env)
session = env['rack.session']
flash = session["flash"] ||= ::ActionController::Flash::FlashHash.new
flash[:notice] = @restart_message
end
def self.redirect_target(env)
referer, host = env["HTTP_REFERER"], env["HTTP_HOST"]
referer && referer.index(host) >= 0 ? referer : @default_target
end
def self.redirect(location)
[302, {"Content-Type" => "text/html", "Location" => location}, [%{<html><body><p><a href="#{location}">Redirecting...</a></p></body></html>}]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment