nkryptic (owner)

Revisions

gist: 87935 Download_button fork
public
Public Clone URL: git://gist.github.com/87935.git
Embed All Files: show embed
passenger_restarter.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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