1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib/puma/cli.rb
2011-09-27 10:53:45 -07:00

100 lines
2.2 KiB
Ruby

require 'optparse'
require 'uri'
require 'puma/configurator'
require 'puma/const'
module Puma
class CLI
DefaultTCPHost = "0.0.0.0"
DefaultTCPPort = 3000
def initialize(argv, stdout=STDOUT)
@argv = argv
@stdout = stdout
setup_options
end
def setup_options
@options = {
:concurrency => 16
}
@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
end
end
@parser.banner = "puma <options> <rackup file>"
@parser.on_tail "-h", "--help", "Show help" do
@stdout.puts @parser
exit 1
end
end
def load_rackup
@app, options = Rack::Builder.parse_file @rackup
@options.merge! options
end
def run
@parser.parse! @argv
@rackup = ARGV.shift || "config.ru"
unless File.exists?(@rackup)
raise "Missing rackup file '#{@rackup}'"
end
load_rackup
if @binds.empty?
@options[:Host] ||= DefaultTCPHost
@options[:Port] ||= DefaultTCPPort
end
server = Puma::Server.new @app, @options[:concurrency]
@stdout.puts "Puma #{Puma::Const::PUMA_VERSION} starting..."
if @options[:Host]
@stdout.puts "Listening on tcp://#{@options[:Host]}:#{@options[:Port]}"
server.add_tcp_listener @options[:Host], @options[:Port]
end
@binds.each do |str|
uri = URI.parse str
case uri.scheme
when "tcp"
@stdout.puts "Listening on #{str}"
server.add_tcp_listener uri.host, uri.port
when "unix"
@stdout.puts "Listening on #{str}"
if uri.host
path = "#{uri.host}/#{uri.path}"
else
path = uri.path
end
server.add_unix_listener path
else
@stdout.puts "Invalid URI: #{str}"
exit 1
end
end
@stdout.puts "Use Ctrl-C to stop"
server.run.join
end
end
end