chneukirchen / rack

a modular Ruby webserver interface

This URL has Read+Write access

eTM (author)
Fri Apr 10 19:52:07 -0700 2009
chneukirchen (committer)
Sat Apr 11 02:54:21 -0700 2009
rack / bin / rackup
100755 177 lines (142 sloc) 3.88 kb
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env ruby
# -*- ruby -*-
 
$LOAD_PATH.unshift File.expand_path("#{__FILE__}/../../lib")
autoload :Rack, 'rack'
 
require 'optparse'
 
automatic = false
server = nil
env = "development"
daemonize = false
pid = nil
options = {:Port => 9292, :Host => "0.0.0.0", :AccessLog => []}
 
# Don't evaluate CGI ISINDEX parameters.
# http://hoohoo.ncsa.uiuc.edu/cgi/cl.html
ARGV.clear if ENV.include?("REQUEST_METHOD")
 
opts = OptionParser.new("", 24, ' ') { |opts|
  opts.banner = "Usage: rackup [ruby options] [rack options] [rackup config]"
 
  opts.separator ""
  opts.separator "Ruby options:"
 
  lineno = 1
  opts.on("-e", "--eval LINE", "evaluate a LINE of code") { |line|
    eval line, TOPLEVEL_BINDING, "-e", lineno
    lineno += 1
  }
 
  opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") {
    $DEBUG = true
  }
  opts.on("-w", "--warn", "turn warnings on for your script") {
    $-w = true
  }
 
  opts.on("-I", "--include PATH",
          "specify $LOAD_PATH (may be used more than once)") { |path|
    $LOAD_PATH.unshift(*path.split(":"))
  }
 
  opts.on("-r", "--require LIBRARY",
          "require the library, before executing your script") { |library|
    require library
  }
 
  opts.separator ""
  opts.separator "Rack options:"
  opts.on("-s", "--server SERVER", "serve using SERVER (webrick/mongrel)") { |s|
    server = s
  }
 
  opts.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") { |host|
    options[:Host] = host
  }
 
  opts.on("-p", "--port PORT", "use PORT (default: 9292)") { |port|
    options[:Port] = port
  }
 
  opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e|
    env = e
  }
 
  opts.on("-D", "--daemonize", "run daemonized in the background") { |d|
    daemonize = d ? true : false
  }
 
  opts.on("-P", "--pid FILE", "file to store PID (default: rack.pid)") { |f|
    pid = File.expand_path(f)
  }
 
  opts.separator ""
  opts.separator "Common options:"
 
  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
 
  opts.on_tail("--version", "Show version") do
    puts "Rack #{Rack.version}"
    exit
  end
 
  opts.parse! ARGV
}
 
require 'pp' if $DEBUG
 
config = ARGV[0] || "config.ru"
if !File.exist? config
  abort "configuration #{config} not found"
end
 
if config =~ /\.ru$/
  cfgfile = File.read(config)
  if cfgfile[/^#\\(.*)/]
    opts.parse! $1.split(/\s+/)
  end
  inner_app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app",
                   nil, config
else
  require config
  inner_app = Object.const_get(File.basename(config, '.rb').capitalize)
end
 
unless server = Rack::Handler.get(server)
  # Guess.
  if ENV.include?("PHP_FCGI_CHILDREN")
    server = Rack::Handler::FastCGI
 
    # We already speak FastCGI
    options.delete :File
    options.delete :Port
  elsif ENV.include?("REQUEST_METHOD")
    server = Rack::Handler::CGI
  else
    begin
      server = Rack::Handler::Mongrel
    rescue LoadError => e
      server = Rack::Handler::WEBrick
    end
  end
end
 
p server if $DEBUG
 
case env
when "development"
  app = Rack::Builder.new {
    use Rack::CommonLogger, $stderr unless server.name =~ /CGI/
    use Rack::ShowExceptions
    use Rack::Lint
    run inner_app
  }.to_app
 
when "deployment"
  app = Rack::Builder.new {
    use Rack::CommonLogger, $stderr unless server.name =~ /CGI/
    run inner_app
  }.to_app
 
when "none"
  app = inner_app
 
end
 
if $DEBUG
  pp app
  pp inner_app
end
 
if daemonize
  if RUBY_VERSION < "1.9"
    exit if fork
    Process.setsid
    exit if fork
    Dir.chdir "/"
    File.umask 0000
    STDIN.reopen "/dev/null"
    STDOUT.reopen "/dev/null", "a"
    STDERR.reopen "/dev/null", "a"
  else
    Process.daemon
  end
 
  if pid
    File.open(pid, 'w'){ |f| f.write("#{Process.pid}") }
    at_exit { File.delete(pid) if File.exist?(pid) }
  end
end
 
server.run app, options