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

189 lines
4.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'
require 'rack/commonlogger'
2011-09-27 12:23:03 -04:00
module Puma
2011-12-01 18:23:14 -05:00
# Handles invoke a Puma::Server in a command line style.
#
2011-09-27 12:23:03 -04:00
class CLI
2011-09-27 13:53:45 -04:00
DefaultTCPHost = "0.0.0.0"
DefaultTCPPort = 9292
2011-09-27 12:23:03 -04:00
2011-12-01 18:23:14 -05:00
# Create a new CLI object using +argv+ as the command line
# arguments.
#
# +stdout+ and +stderr+ can be set to IO-like objects which
# this object will report status on.
#
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
2011-09-27 17:33:17 -04:00
@events = Events.new @stdout, @stderr
2011-09-27 12:23:03 -04:00
setup_options
end
2011-12-01 18:23:14 -05:00
# Write +str+ to +@stdout+
#
def log(str)
@stdout.puts str
end
2011-12-01 18:23:14 -05:00
# Write +str+ to +@stderr+
#
def error(str)
@stderr.puts "ERROR: #{str}"
exit 1
end
2011-12-01 18:23:14 -05:00
# Build the OptionParser object to handle the available options.
#
2011-09-27 12:23:03 -04:00
def setup_options
2011-09-27 13:53:45 -04:00
@options = {
2011-09-27 17:33:17 -04:00
:min_threads => 0,
:max_threads => 16,
:quiet => false
2011-09-27 13:53:45 -04:00
}
2011-09-27 12:23:03 -04:00
2011-09-27 13:53:45 -04:00
@binds = []
@parser = OptionParser.new do |o|
o.on "-b", "--bind URI", "URI to bind to (tcp:// and unix:// only)" do |arg|
@binds << arg
end
o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|
@options[:pidfile] = arg
end
o.on "-q", "--quiet", "Quiet down the output" do
@options[:quiet] = true
end
2011-09-27 17:33:17 -04:00
o.on '-t', '--threads INT', "min:max threads to use (default 0:16)" do |arg|
min, max = arg.split(":")
if max
@options[:min_threads] = min.to_i
@options[:max_threads] = max.to_i
else
@options[:min_threads] = 0
@options[:max_threads] = arg.to_i
end
2011-09-27 13:53:45 -04:00
end
2011-09-27 12:23:03 -04:00
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-12-01 18:23:14 -05:00
# Load the specified rackup file, pull an options from
# the rackup file, and set @app.
#
2011-09-27 13:53:45 -04:00
def load_rackup
@app, options = Rack::Builder.parse_file @rackup
@options.merge! options
2011-09-27 14:44:21 -04:00
options.each do |key,val|
if key.to_s[0,4] == "bind"
@binds << val
end
end
2011-09-27 13:53:45 -04:00
end
2011-12-01 18:23:14 -05:00
# If configured, write the pid of the current process out
# to a file.
#
2011-11-22 00:15:40 -05:00
def write_pid
if path = @options[:pidfile]
File.open(path, "w") do |f|
f.puts Process.pid
end
end
end
2011-12-01 18:23:14 -05:00
# :nodoc:
2011-11-22 00:15:40 -05:00
def parse_options
2011-09-27 13:53:45 -04:00
@parser.parse! @argv
2011-11-22 00:15:40 -05:00
end
2011-12-01 18:23:14 -05:00
# Parse the options, load the rackup, start the server and wait
# for it to finish.
#
2011-11-22 00:15:40 -05:00
def run
parse_options
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
2011-11-22 00:15:40 -05:00
write_pid
2011-09-27 13:53:45 -04:00
2011-11-30 18:07:07 -05:00
unless @options[:quiet]
@app = Rack::CommonLogger.new(@app, STDOUT)
end
2011-09-27 13:53:45 -04:00
if @binds.empty?
@options[:Host] ||= DefaultTCPHost
@options[:Port] ||= DefaultTCPPort
end
2011-09-27 17:33:17 -04:00
min_t = @options[:min_threads]
max_t = @options[:max_threads]
server = Puma::Server.new @app, @events
server.min_threads = min_t
server.max_threads = max_t
2011-09-27 13:53:45 -04:00
log "Puma #{Puma::Const::PUMA_VERSION} starting..."
2011-09-27 17:33:17 -04:00
log "* Min threads: #{min_t}, max threads: #{max_t}"
2011-09-27 13:53:45 -04:00
if @options[:Host]
2011-09-27 17:33:17 -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 17:33:17 -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 17:33:17 -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
error "Invalid URI: #{str}"
2011-09-27 12:23:03 -04:00
end
end
if server.attempt_bonjour(@rackup)
log "* Announced services via bonjour"
end
log "Use Ctrl-C to stop"
2011-09-27 12:23:03 -04:00
begin
server.run.join
rescue Interrupt
log " - Shutting down..."
end
2011-09-27 12:23:03 -04:00
end
end
end