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 = {
|
|
|
|
:Host => '0.0.0.0',
|
|
|
|
:Port => 8080,
|
|
|
|
:Threads => '0:16',
|
2011-12-19 17:13:18 -05:00
|
|
|
:Verbose => 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
|
|
|
|
2011-12-19 17:13:18 -05:00
|
|
|
if options[:Verbose]
|
2011-11-22 18:06:51 -05:00
|
|
|
app = Rack::CommonLogger.new(app, STDOUT)
|
|
|
|
end
|
|
|
|
|
2012-07-05 17:07:45 -04:00
|
|
|
if options[:environment]
|
|
|
|
ENV['RACK_ENV'] = options[:environment].to_s
|
|
|
|
end
|
|
|
|
|
2011-10-04 17:23:51 -04:00
|
|
|
server = ::Puma::Server.new(app)
|
|
|
|
min, max = options[:Threads].split(':', 2)
|
|
|
|
|
2011-11-22 18:06:51 -05:00
|
|
|
puts "Puma #{::Puma::Const::PUMA_VERSION} starting..."
|
|
|
|
puts "* Min threads: #{min}, max threads: #{max}"
|
2012-07-05 17:07:45 -04:00
|
|
|
puts "* Environment: #{ENV['RACK_ENV']}"
|
2011-11-22 18:06:51 -05:00
|
|
|
puts "* Listening on tcp://#{options[:Host]}:#{options[:Port]}"
|
|
|
|
|
2011-10-04 17:23:51 -04:00
|
|
|
server.add_tcp_listener options[:Host], options[:Port]
|
2013-07-16 20:08:30 -04:00
|
|
|
server.min_threads = min
|
|
|
|
server.max_threads = max
|
2011-10-04 17:23:51 -04:00
|
|
|
yield server if block_given?
|
|
|
|
|
2012-03-25 17:13:11 -04:00
|
|
|
begin
|
|
|
|
server.run.join
|
|
|
|
rescue Interrupt
|
|
|
|
puts "* Gracefully stopping, waiting for requests to finish"
|
|
|
|
server.stop(true)
|
|
|
|
puts "* Goodbye!"
|
|
|
|
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)",
|
|
|
|
"Quiet" => "Don't report each request"
|
2011-10-04 17:23:51 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
register :puma, Puma
|
|
|
|
end
|
|
|
|
end
|
2012-10-20 02:03:02 -04:00
|
|
|
|