From 7c41cf7bb7d76658157e94813b608a470bdd6b30 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Tue, 4 Oct 2011 14:23:51 -0700 Subject: [PATCH] add Rack handler for Puma --- lib/rack/handler/puma.rb | 33 +++++++++++++++++++++++++++++++++ test/test_rack_handler.rb | 10 ++++++++++ 2 files changed, 43 insertions(+) create mode 100644 lib/rack/handler/puma.rb create mode 100644 test/test_rack_handler.rb diff --git a/lib/rack/handler/puma.rb b/lib/rack/handler/puma.rb new file mode 100644 index 00000000..99fb7ee8 --- /dev/null +++ b/lib/rack/handler/puma.rb @@ -0,0 +1,33 @@ +require 'rack/handler' +require 'puma' + +module Rack + module Handler + module Puma + DEFAULT_OPTIONS = {:Host => '0.0.0.0', :Port => 8080, :Threads => '0:16'} + + def self.run(app, options = {}) + options = DEFAULT_OPTIONS.merge(options) + server = ::Puma::Server.new(app) + min, max = options[:Threads].split(':', 2) + + server.add_tcp_listener options[:Host], options[:Port] + server.min_threads = Integer(min) + server.max_threads = Integer(max) + yield server if block_given? + + server.run.join + end + + def self.valid_options + { + "Host=HOST" => "Hostname to listen on (default: localhost)", + "Port=PORT" => "Port to listen on (default: 8080)", + "Threads=MIN:MAX" => "min:max threads to use (default 0:16)" + } + end + end + + register :puma, Puma + end +end diff --git a/test/test_rack_handler.rb b/test/test_rack_handler.rb new file mode 100644 index 00000000..51c56e44 --- /dev/null +++ b/test/test_rack_handler.rb @@ -0,0 +1,10 @@ +require 'test/unit' + +class TestPumaUnixSocket < Test::Unit::TestCase + def test_handler + handler = Rack::Handler.get(:puma) + assert_equal Rack::Handler::Puma, handler + handler = Rack::Handler.get('Puma') + assert_equal Rack::Handler::Puma, handler + end +end