2011-10-04 17:23:51 -04:00
|
|
|
require 'rack/handler'
|
|
|
|
|
|
|
|
module Rack
|
|
|
|
module Handler
|
|
|
|
module Puma
|
2011-11-22 18:06:51 -05:00
|
|
|
DEFAULT_OPTIONS = {
|
2015-12-02 21:08:37 -05:00
|
|
|
:Verbose => false,
|
2016-02-04 17:11:34 -05:00
|
|
|
:Silent => false
|
2011-11-22 18:06:51 -05:00
|
|
|
}
|
2011-10-04 17:23:51 -04:00
|
|
|
|
2017-02-27 16:52:43 -05:00
|
|
|
def self.config(app, options = {})
|
2016-09-06 16:21:08 -04:00
|
|
|
require 'puma/configuration'
|
|
|
|
require 'puma/events'
|
|
|
|
require 'puma/launcher'
|
|
|
|
|
2017-03-03 17:04:56 -05:00
|
|
|
default_options = DEFAULT_OPTIONS.dup
|
|
|
|
|
|
|
|
# Libraries pass in values such as :Port and there is no way to determine
|
|
|
|
# if it is a default provided by the library or a special value provided
|
|
|
|
# by the user. A special key `user_supplied_options` can be passed. This
|
|
|
|
# contains an array of all explicitly defined user options. We then
|
|
|
|
# know that all other values are defaults
|
|
|
|
if user_supplied_options = options.delete(:user_supplied_options)
|
|
|
|
(options.keys - user_supplied_options).each do |k, v|
|
|
|
|
default_options[k] = options.delete(k)
|
|
|
|
end
|
|
|
|
end
|
2011-11-22 18:06:51 -05:00
|
|
|
|
2017-03-03 17:04:56 -05:00
|
|
|
conf = ::Puma::Configuration.new(options, default_options) do |user_config, file_config, default_config|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.quiet
|
2016-02-25 16:09:02 -05:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
if options.delete(:Verbose)
|
|
|
|
app = Rack::CommonLogger.new(app, STDOUT)
|
|
|
|
end
|
2011-11-22 18:06:51 -05:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
if options[:environment]
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.environment options[:environment]
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if options[:Threads]
|
|
|
|
min, max = options.delete(:Threads).split(':', 2)
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.threads min, max
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2012-07-05 17:07:45 -04:00
|
|
|
|
2017-05-01 12:54:53 -04:00
|
|
|
host = options[:Host] || default_options[:Host]
|
|
|
|
port = options[:Port] || default_options[:Port]
|
|
|
|
|
|
|
|
self.set_host_port_to_config(host, port, user_config)
|
2017-03-03 17:04:56 -05:00
|
|
|
self.set_host_port_to_config(default_options[:Host], default_options[:Port], default_config)
|
2016-02-06 22:00:29 -05:00
|
|
|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.app app
|
2016-02-04 17:11:34 -05:00
|
|
|
end
|
2017-02-27 16:52:43 -05:00
|
|
|
conf
|
|
|
|
end
|
|
|
|
|
2017-03-03 17:04:56 -05:00
|
|
|
|
|
|
|
|
2017-02-27 16:52:43 -05:00
|
|
|
def self.run(app, options = {})
|
|
|
|
conf = self.config(app, options)
|
2011-10-04 17:23:51 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
events = options.delete(:Silent) ? ::Puma::Events.strings : ::Puma::Events.stdio
|
|
|
|
|
|
|
|
launcher = ::Puma::Launcher.new(conf, :events => events)
|
2011-10-04 17:23:51 -04:00
|
|
|
|
2016-02-04 10:25:04 -05:00
|
|
|
yield launcher if block_given?
|
2012-03-25 17:13:11 -04:00
|
|
|
begin
|
2016-02-04 10:25:04 -05:00
|
|
|
launcher.run
|
2012-03-25 17:13:11 -04:00
|
|
|
rescue Interrupt
|
2016-02-04 10:25:04 -05:00
|
|
|
puts "* Gracefully stopping, waiting for requests to finish"
|
|
|
|
launcher.stop
|
|
|
|
puts "* Goodbye!"
|
2012-03-25 17:13:11 -04:00
|
|
|
end
|
2011-10-04 17:23:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_options
|
|
|
|
{
|
|
|
|
"Host=HOST" => "Hostname to listen on (default: localhost)",
|
|
|
|
"Port=PORT" => "Port to listen on (default: 8080)",
|
2011-11-22 18:06:51 -05:00
|
|
|
"Threads=MIN:MAX" => "min:max threads to use (default 0:16)",
|
2015-10-20 08:55:42 -04:00
|
|
|
"Verbose" => "Don't report each request (default: false)"
|
2011-10-04 17:23:51 -04:00
|
|
|
}
|
|
|
|
end
|
2017-03-03 17:04:56 -05:00
|
|
|
private
|
|
|
|
def self.set_host_port_to_config(host, port, config)
|
|
|
|
if host && (host[0,1] == '.' || host[0,1] == '/')
|
|
|
|
config.bind "unix://#{host}"
|
|
|
|
elsif host && host =~ /^ssl:\/\//
|
|
|
|
uri = URI.parse(host)
|
|
|
|
uri.port ||= port || ::Puma::Configuration::DefaultTCPPort
|
|
|
|
config.bind uri.to_s
|
|
|
|
else
|
|
|
|
|
|
|
|
if host
|
|
|
|
port ||= ::Puma::Configuration::DefaultTCPPort
|
|
|
|
end
|
|
|
|
|
|
|
|
if port
|
|
|
|
host ||= ::Puma::Configuration::DefaultTCPHost
|
|
|
|
config.port port, host
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-10-04 17:23:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
register :puma, Puma
|
|
|
|
end
|
|
|
|
end
|