2011-10-04 17:23:51 -04:00
|
|
|
require 'rack/handler'
|
|
|
|
require 'puma'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def self.run(app, options = {})
|
|
|
|
options = DEFAULT_OPTIONS.merge(options)
|
2011-11-22 18:06:51 -05:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
conf = ::Puma::Configuration.new do |c|
|
|
|
|
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]
|
|
|
|
c.environment options[:environment]
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:Threads]
|
|
|
|
min, max = options.delete(:Threads).split(':', 2)
|
|
|
|
c.threads min, max
|
|
|
|
end
|
2012-07-05 17:07:45 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
host = options[:Host] || ::Puma::Configuration::DefaultTCPHost
|
|
|
|
port = options[:Port] || ::Puma::Configuration::DefaultTCPPort
|
|
|
|
|
|
|
|
c.port port, host
|
|
|
|
|
|
|
|
c.app app
|
2016-02-04 17:11:34 -05:00
|
|
|
end
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
register :puma, Puma
|
|
|
|
end
|
|
|
|
end
|
2012-10-20 02:03:02 -04:00
|
|
|
|