Skip to content

Commit 4c9512d

Browse files
authored
Use conventional priority for setting configurable values (#202)
* Use conventional priority for setting configurable values
1 parent 6b459c1 commit 4c9512d

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

src/amqproxy/cli.cr

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ require "log"
99
class AMQProxy::CLI
1010
Log = ::Log.for(self)
1111

12-
@listen_address = ENV["LISTEN_ADDRESS"]? || "localhost"
13-
@listen_port = ENV["LISTEN_PORT"]? || 5673
14-
@http_port = ENV["HTTP_PORT"]? || 15673
12+
@listen_address = "localhost"
13+
@listen_port = 5673
14+
@http_port = 15673
1515
@log_level : ::Log::Severity = ::Log::Severity::Info
16-
@idle_connection_timeout : Int32 = ENV.fetch("IDLE_CONNECTION_TIMEOUT", "5").to_i
16+
@idle_connection_timeout : Int32 = 5
1717
@term_timeout = -1
1818
@term_client_close_timeout = 0
19-
@upstream = ENV["AMQP_URL"]?
2019
@server : AMQProxy::Server? = nil
2120

2221
def parse_config(path) # ameba:disable Metrics/CyclomaticComplexity
@@ -36,7 +35,7 @@ class AMQProxy::CLI
3635
when "listen"
3736
section.each do |key, value|
3837
case key
39-
when "port" then @listen_port = value
38+
when "port" then @listen_port = value.to_i
4039
when "bind", "address" then @listen_address = value
4140
when "log_level" then @log_level = ::Log::Severity.parse(value)
4241
else raise "Unsupported config #{name}/#{key}"
@@ -49,27 +48,46 @@ class AMQProxy::CLI
4948
abort ex.message
5049
end
5150

51+
def apply_env_variables
52+
@listen_address = ENV["LISTEN_ADDRESS"]? || @listen_address
53+
@listen_port = ENV["LISTEN_PORT"]?.try &.to_i || @listen_port
54+
@http_port = ENV["HTTP_PORT"]?.try &.to_i || @http_port
55+
@log_level = ENV["LOG_LEVEL"]?.try { |level| ::Log::Severity.parse(level) } || @log_level
56+
@idle_connection_timeout = ENV["IDLE_CONNECTION_TIMEOUT"]?.try &.to_i || @idle_connection_timeout
57+
@term_timeout = ENV["TERM_TIMEOUT"]?.try &.to_i || @term_timeout
58+
@term_client_close_timeout = ENV["TERM_CLIENT_CLOSE_TIMEOUT"]?.try &.to_i || @term_client_close_timeout
59+
@upstream = ENV["AMQP_URL"]? || @upstream
60+
end
61+
5262
def run(argv)
5363
raise "run cant be called multiple times" unless @server.nil?
5464

65+
# Parse config file first
66+
OptionParser.parse(argv) do |parser|
67+
parser.on("-c FILE", "--config=FILE", "Load config file") { |v| parse_config(v) }
68+
parser.invalid_option { } # Invalid arguments are handled by the next OptionParser
69+
end
70+
71+
apply_env_variables
72+
73+
# Parse CLI arguments
5574
p = OptionParser.parse(argv) do |parser|
5675
parser.banner = "Usage: amqproxy [options] [amqp upstream url]"
5776
parser.on("-l ADDRESS", "--listen=ADDRESS", "Address to listen on (default is localhost)") do |v|
5877
@listen_address = v
5978
end
6079
parser.on("-p PORT", "--port=PORT", "Port to listen on (default: 5673)") { |v| @listen_port = v.to_i }
6180
parser.on("-b PORT", "--http-port=PORT", "HTTP Port to listen on (default: 15673)") { |v| @http_port = v.to_i }
62-
parser.on("-t IDLE_CONNECTION_TIMEOUT", "--idle-connection-timeout=SECONDS", "Maxiumum time in seconds an unused pooled connection stays open (default 5s)") do |v|
81+
parser.on("-t IDLE_CONNECTION_TIMEOUT", "--idle-connection-timeout=SECONDS", "Maximum time in seconds an unused pooled connection stays open (default 5s)") do |v|
6382
@idle_connection_timeout = v.to_i
6483
end
6584
parser.on("--term-timeout=SECONDS", "At TERM the server waits SECONDS seconds for clients to gracefully close their sockets after Close has been sent (default: infinite)") do |v|
6685
@term_timeout = v.to_i
6786
end
68-
parser.on("--term-client-close-timeout=SECONDS", "At TERM the server waits SECONDS seconds for clients to send Close beforing sending Close to clients (default: 0s)") do |v|
87+
parser.on("--term-client-close-timeout=SECONDS", "At TERM the server waits SECONDS seconds for clients to send Close before sending Close to clients (default: 0s)") do |v|
6988
@term_client_close_timeout = v.to_i
7089
end
7190
parser.on("-d", "--debug", "Verbose logging") { @log_level = ::Log::Severity::Debug }
72-
parser.on("-c FILE", "--config=FILE", "Load config file") { |v| parse_config(v) }
7391
parser.on("-h", "--help", "Show this help") { puts parser.to_s; exit 0 }
7492
parser.on("-v", "--version", "Display version") { puts AMQProxy::VERSION.to_s; exit 0 }
7593
parser.invalid_option { |arg| abort "Invalid argument: #{arg}" }

0 commit comments

Comments
 (0)