unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob ebf57c3c323c2295bc18c1d6af9bb63df47e973c 5093 bytes (raw)
$ git show v0.1.0:bin/unicorn	# shows this blob on the CLI

  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
178
179
180
181
182
183
184
185
186
187
188
189
 
#!/home/ew/bin/ruby
$stdin.sync = $stdout.sync = $stderr.sync = true
require 'unicorn' # require this first to populate Unicorn::DEFAULT_START_CTX
require 'optparse'

env = "development"
daemonize = false
listeners = []
options = { :listeners => listeners }
host = Unicorn::Const::DEFAULT_HOST
port = Unicorn::Const::DEFAULT_PORT

opts = OptionParser.new("", 24, '  ') do |opts|
  opts.banner = "Usage: #{File.basename($0)} " \
                "[ruby options] [unicorn options] [rackup config file]"

  opts.separator "Ruby options:"

  lineno = 1
  opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
    eval line, TOPLEVEL_BINDING, "-e", lineno
    lineno += 1
  end

  opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
    $DEBUG = true
  end

  opts.on("-w", "--warn", "turn warnings on for your script") do
    $-w = true
  end

  opts.on("-I", "--include PATH",
          "specify $LOAD_PATH (may be used more than once)") do |path|
    $LOAD_PATH.unshift(*path.split(/:/))
  end

  opts.on("-r", "--require LIBRARY",
          "require the library, before executing your script") do |library|
    require library
  end

  opts.separator "Unicorn options:"

  # some of these switches exist for rackup command-line compatibility,

  opts.on("-o", "--host HOST",
          "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
    host = h
  end

  opts.on("-p", "--port PORT",
          "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p|
    port = p.to_i
  end

  opts.on("-E", "--env ENVIRONMENT",
          "use ENVIRONMENT for defaults (default: development)") do |e|
    env = e
  end

  opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
    daemonize = d ? true : false
  end

  opts.on("-P", "--pid FILE", "file to store PID (default: none)") do |f|
    options[:pid] = File.expand_path(f)
  end

  opts.on("-s", "--server SERVER",
          "this flag only exists for compatibility") do |s|
    warn "-s/--server only exists for compatibility with rackup"
  end

  # Unicorn-specific stuff
  opts.on("-l", "--listen {HOST:PORT|PATH}",
          "listen on HOST:PORT or PATH",
          "this may be specified multiple times",
          "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
    listeners << address
  end

  opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
    options[:config_file] = File.expand_path(f)
  end

  # I'm avoiding Unicorn-specific config options on the command-line.
  # IMNSHO, config options on the command-line are redundant given
  # config files and make things unnecessarily complicated with multiple
  # places to look for a config option.

  opts.separator "Common options:"

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end

  opts.on_tail("-v", "--version", "Show version") do
    puts "unicorn v#{Unicorn::Const::UNICORN_VERSION}"
    exit
  end

  opts.parse! ARGV
end

require 'pp' if $DEBUG

# require Rack as late as possible in case $LOAD_PATH is modified
# in config.ru or command-line
require 'rack'

config = ARGV[0] || "config.ru"
abort "configuration file #{config} not found" unless File.exist?(config)

inner_app = case config
when /\.ru$/
  raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
  # parse embedded command-line options in config.ru comments
  if raw[/^#\\(.*)/]
    opts.parse! $1.split(/\s+/)
  end
  lambda { || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) }
else
  lambda do ||
    require config
    Object.const_get(File.basename(config, '.rb').capitalize)
  end
end

app = case env
when "development"
  lambda do ||
    Rack::Builder.new do
      use Rack::CommonLogger, $stderr
      use Rack::ShowExceptions
      use Rack::Lint
      run inner_app.call
    end.to_app
  end
when "deployment"
  lambda do ||
    Rack::Builder.new do
      use Rack::CommonLogger, $stderr
      run inner_app.call
    end.to_app
  end
else
  inner_app
end

if listeners.empty?
  listener = "#{host}:#{port}"
  listeners << listener
end

if $DEBUG
  pp({
    :unicorn_options => options,
    :app => app,
    :inner_app => inner_app,
    :daemonize => daemonize,
  })
end

# only daemonize if we're not inheriting file descriptors from our parent
if daemonize

  $stdin.reopen("/dev/null")
  unless ENV['UNICORN_FD']
    exit if fork
    Process.setsid
    exit if fork
  end

  # We don't do a lot of standard daemonization stuff:
  #   * $stderr/$stderr can/will be redirected separately
  #   * umask is whatever was set by the parent process at startup
  #     and can be set in config.ru and config_file, so making it
  #     0000 and potentially exposing sensitive log data can be bad
  #     policy.
  #   * Don't bother to chdir here since Unicorn is designed to
  #     run inside APP_ROOT.  Unicorn will also re-chdir() to
  #     the directory it was started in when being re-executed
  #     to pickup code changes if the original deployment directory
  #     is a symlink or otherwise got replaced.
end

Unicorn.run(app, options)

git clone https://yhbt.net/unicorn.git