summaryrefslogtreecommitdiffstats
path: root/lib/rack/handler/plum.rb
blob: 599a3e100ea234f964ceb93b9342191ef0f7381c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Rack
  module Handler
    class Plum
      def self.run(app, options = {})
        opts = default_options.merge(options)

        config = ::Plum::Rack::Config.new(
          listeners: [
            {
              listener: ::Plum::Rack::TCPListener,
              hostname: opts[:Host],
              port: opts[:Port].to_i
            }
          ],
          debug: !!opts[:Debug]
        )

        @server = ::Plum::Rack::Server.new(app, config)
        yield @server if block_given?
        @server.start
      end

      def self.valid_options
        {
          "Host=HOST"    => "Hostname to listen on (default: #{default_options[:Host]})",
          "Port=PORT"    => "Port to listen on (default: #{default_options[:Port]})",
          "Debug"        => "Turn on debug mode (default: #{default_options[:Debug]})",
        }
      end

      private
      def self.default_options
        rack_env = ENV["RACK_ENV"] || "development"
        default_options = {
          Host: rack_env == "development" ? "localhost" : "0.0.0.0",
          Port: 8080,
          Debug: true,
        }
      end
    end

    register(:plum, ::Rack::Handler::Plum)
  end
end