Module: PumaPlus::Address

Defined in:
lib/puma_plus/address.rb

Overview

Address parsing shared by the config DSL and the command line.

Its own file because both the launcher and the worker need it, and the worker loads the DSL without the launcher -- it reads the config only for lifecycle hooks. Having the DSL reach back into Launcher made that combination raise NameError on any config using activate_control_app, which no test caught because every launcher test loads the launcher.

Class Method Summary collapse

Class Method Details

.host_port(value, default_port, what) ⇒ Object

Normalize whatever someone typed for an address into "host:port".

Accepts "tcp://127.0.0.1:9293", "127.0.0.1:9293", ":9293" and "9293", because all four are things people type and three of them are not URLs. URI.parse alone rejects the bare forms with a message about RFC 3986 that tells you nothing about what to do instead.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/puma_plus/address.rb', line 20

def host_port(value, default_port, what)
  s = value.to_s.strip.sub(%r{\A[a-zA-Z][a-zA-Z0-9+.-]*://}, "")
  case s
  when /\A\[([^\]]+)\]:(\d+)\z/ then "[#{Regexp.last_match(1)}]:#{Regexp.last_match(2)}"
  when /\A(.*):(\d+)\z/
    host = Regexp.last_match(1)
    "#{host.empty? ? '0.0.0.0' : host}:#{Regexp.last_match(2)}"
  when /\A\d+\z/ then "0.0.0.0:#{s}"
  when "" then raise(ConfigError, "#{what}: expected an address like 127.0.0.1:#{default_port}")
  else "#{s}:#{default_port}"
  end
end