2011-10-04 14:23:51 -07:00
|
|
|
require 'rack/handler'
|
|
|
|
|
|
|
|
module Rack
|
|
|
|
module Handler
|
|
|
|
module Puma
|
2011-11-22 15:06:51 -08:00
|
|
|
DEFAULT_OPTIONS = {
|
2015-12-03 05:08:37 +03:00
|
|
|
:Verbose => false,
|
2016-02-04 16:11:34 -06:00
|
|
|
:Silent => false
|
2011-11-22 15:06:51 -08:00
|
|
|
}
|
2011-10-04 14:23:51 -07:00
|
|
|
|
|
|
|
def self.run(app, options = {})
|
2016-09-06 13:21:08 -07:00
|
|
|
require 'puma/configuration'
|
|
|
|
require 'puma/commonlogger'
|
|
|
|
require 'puma/events'
|
|
|
|
require 'puma/launcher'
|
|
|
|
|
2011-10-04 14:23:51 -07:00
|
|
|
options = DEFAULT_OPTIONS.merge(options)
|
2011-11-22 15:06:51 -08:00
|
|
|
|
2016-03-26 20:18:36 +05:30
|
|
|
conf = ::Puma::Configuration.new(options) do |c|
|
2016-02-25 13:09:02 -08:00
|
|
|
c.quiet
|
|
|
|
|
2016-02-06 19:00:29 -08:00
|
|
|
if options.delete(:Verbose)
|
|
|
|
app = Rack::CommonLogger.new(app, STDOUT)
|
|
|
|
end
|
2011-11-22 15:06:51 -08:00
|
|
|
|
2016-02-06 19:00:29 -08:00
|
|
|
if options[:environment]
|
|
|
|
c.environment options[:environment]
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:Threads]
|
|
|
|
min, max = options.delete(:Threads).split(':', 2)
|
|
|
|
c.threads min, max
|
|
|
|
end
|
2012-07-05 22:07:45 +01:00
|
|
|
|
2016-02-25 13:42:29 -08:00
|
|
|
host = options[:Host]
|
2016-02-06 19:00:29 -08:00
|
|
|
|
2016-02-25 13:42:29 -08:00
|
|
|
if host && (host[0,1] == '.' || host[0,1] == '/')
|
|
|
|
c.bind "unix://#{host}"
|
|
|
|
else
|
|
|
|
host ||= ::Puma::Configuration::DefaultTCPHost
|
|
|
|
port = options[:Port] || ::Puma::Configuration::DefaultTCPPort
|
|
|
|
|
|
|
|
c.port port, host
|
|
|
|
end
|
2016-02-06 19:00:29 -08:00
|
|
|
|
|
|
|
c.app app
|
2016-02-04 16:11:34 -06:00
|
|
|
end
|
2011-10-04 14:23:51 -07:00
|
|
|
|
2016-02-06 19:00:29 -08:00
|
|
|
events = options.delete(:Silent) ? ::Puma::Events.strings : ::Puma::Events.stdio
|
|
|
|
|
|
|
|
launcher = ::Puma::Launcher.new(conf, :events => events)
|
2011-10-04 14:23:51 -07:00
|
|
|
|
2016-02-04 09:25:04 -06:00
|
|
|
yield launcher if block_given?
|
2012-03-26 00:13:11 +03:00
|
|
|
begin
|
2016-02-04 09:25:04 -06:00
|
|
|
launcher.run
|
2012-03-26 00:13:11 +03:00
|
|
|
rescue Interrupt
|
2016-02-04 09:25:04 -06:00
|
|
|
puts "* Gracefully stopping, waiting for requests to finish"
|
|
|
|
launcher.stop
|
|
|
|
puts "* Goodbye!"
|
2012-03-26 00:13:11 +03:00
|
|
|
end
|
2011-10-04 14:23:51 -07: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 15:06:51 -08:00
|
|
|
"Threads=MIN:MAX" => "min:max threads to use (default 0:16)",
|
2015-10-20 14:55:42 +02:00
|
|
|
"Verbose" => "Don't report each request (default: false)"
|
2011-10-04 14:23:51 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
register :puma, Puma
|
|
|
|
end
|
|
|
|
end
|