2011-09-27 12:23:03 -04:00
|
|
|
require 'optparse'
|
2011-09-27 13:53:45 -04:00
|
|
|
require 'uri'
|
|
|
|
|
2011-09-27 14:07:56 -04:00
|
|
|
require 'puma/server'
|
2011-09-27 12:23:03 -04:00
|
|
|
require 'puma/const'
|
|
|
|
|
|
|
|
module Puma
|
|
|
|
class CLI
|
2011-09-27 13:53:45 -04:00
|
|
|
DefaultTCPHost = "0.0.0.0"
|
|
|
|
DefaultTCPPort = 3000
|
2011-09-27 12:23:03 -04:00
|
|
|
|
2011-09-27 14:07:56 -04:00
|
|
|
def initialize(argv, stdout=STDOUT, stderr=STDERR)
|
2011-09-27 12:23:03 -04:00
|
|
|
@argv = argv
|
|
|
|
@stdout = stdout
|
2011-09-27 14:07:56 -04:00
|
|
|
@stderr = stderr
|
2011-09-27 12:23:03 -04:00
|
|
|
|
|
|
|
setup_options
|
|
|
|
end
|
|
|
|
|
2011-09-27 14:07:56 -04:00
|
|
|
def log(str)
|
|
|
|
@stdout.puts str
|
|
|
|
end
|
|
|
|
|
|
|
|
def error(str)
|
|
|
|
@stderr.puts "ERROR: #{str}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2011-09-27 12:23:03 -04:00
|
|
|
def setup_options
|
2011-09-27 13:53:45 -04:00
|
|
|
@options = {
|
|
|
|
:concurrency => 16
|
|
|
|
}
|
2011-09-27 12:23:03 -04:00
|
|
|
|
2011-09-27 13:53:45 -04:00
|
|
|
@binds = []
|
|
|
|
|
|
|
|
@parser = OptionParser.new do |o|
|
|
|
|
o.on '-n', '--concurrency INT', "Number of concurrent threads to use" do |arg|
|
|
|
|
@options[:concurrency] = arg.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
o.on "-b", "--bind URI", "URI to bind to (tcp:// and unix:// only)" do |arg|
|
|
|
|
@binds << arg
|
2011-09-27 12:23:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-27 13:53:45 -04:00
|
|
|
@parser.banner = "puma <options> <rackup file>"
|
2011-09-27 12:23:03 -04:00
|
|
|
|
2011-09-27 13:53:45 -04:00
|
|
|
@parser.on_tail "-h", "--help", "Show help" do
|
2011-09-27 14:07:56 -04:00
|
|
|
log @parser
|
2011-09-27 12:23:03 -04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-27 13:53:45 -04:00
|
|
|
def load_rackup
|
|
|
|
@app, options = Rack::Builder.parse_file @rackup
|
|
|
|
@options.merge! options
|
|
|
|
end
|
|
|
|
|
2011-09-27 12:23:03 -04:00
|
|
|
def run
|
2011-09-27 13:53:45 -04:00
|
|
|
@parser.parse! @argv
|
2011-09-27 12:23:03 -04:00
|
|
|
|
|
|
|
@rackup = ARGV.shift || "config.ru"
|
|
|
|
|
|
|
|
unless File.exists?(@rackup)
|
|
|
|
raise "Missing rackup file '#{@rackup}'"
|
|
|
|
end
|
|
|
|
|
2011-09-27 13:53:45 -04:00
|
|
|
load_rackup
|
|
|
|
|
|
|
|
if @binds.empty?
|
|
|
|
@options[:Host] ||= DefaultTCPHost
|
|
|
|
@options[:Port] ||= DefaultTCPPort
|
|
|
|
end
|
|
|
|
|
|
|
|
server = Puma::Server.new @app, @options[:concurrency]
|
|
|
|
|
2011-09-27 14:07:56 -04:00
|
|
|
log "Puma #{Puma::Const::PUMA_VERSION} starting..."
|
2011-09-27 13:53:45 -04:00
|
|
|
|
|
|
|
if @options[:Host]
|
2011-09-27 14:07:56 -04:00
|
|
|
log "Listening on tcp://#{@options[:Host]}:#{@options[:Port]}"
|
2011-09-27 13:53:45 -04:00
|
|
|
server.add_tcp_listener @options[:Host], @options[:Port]
|
|
|
|
end
|
|
|
|
|
|
|
|
@binds.each do |str|
|
|
|
|
uri = URI.parse str
|
|
|
|
case uri.scheme
|
|
|
|
when "tcp"
|
2011-09-27 14:07:56 -04:00
|
|
|
log "Listening on #{str}"
|
2011-09-27 13:53:45 -04:00
|
|
|
server.add_tcp_listener uri.host, uri.port
|
|
|
|
when "unix"
|
2011-09-27 14:07:56 -04:00
|
|
|
log "Listening on #{str}"
|
2011-09-27 14:11:46 -04:00
|
|
|
path = "#{uri.host}#{uri.path}"
|
2011-09-27 12:23:03 -04:00
|
|
|
|
2011-09-27 13:53:45 -04:00
|
|
|
server.add_unix_listener path
|
|
|
|
else
|
2011-09-27 14:07:56 -04:00
|
|
|
error "Invalid URI: #{str}"
|
2011-09-27 12:23:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-27 14:07:56 -04:00
|
|
|
log "Use Ctrl-C to stop"
|
2011-09-27 12:23:03 -04:00
|
|
|
|
2011-09-27 13:53:45 -04:00
|
|
|
server.run.join
|
2011-09-27 12:23:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|