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

106 lines
2.2 KiB
Ruby
Raw Normal View History

2011-09-27 12:23:03 -04:00
require 'optparse'
2011-09-27 13:53:45 -04:00
require 'uri'
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
def initialize(argv, stdout=STDOUT, stderr=STDERR)
2011-09-27 12:23:03 -04:00
@argv = argv
@stdout = stdout
@stderr = stderr
2011-09-27 12:23:03 -04:00
setup_options
end
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
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]
log "Puma #{Puma::Const::PUMA_VERSION} starting..."
2011-09-27 13:53:45 -04:00
if @options[:Host]
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"
log "Listening on #{str}"
2011-09-27 13:53:45 -04:00
server.add_tcp_listener uri.host, uri.port
when "unix"
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
error "Invalid URI: #{str}"
2011-09-27 12:23:03 -04:00
end
end
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