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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/hyperion/cli.rb', line 13
def self.run(argv)
cli_opts = {}
config_path = nil
parser = OptionParser.new do |o|
o.banner = 'Usage: hyperion [options] config.ru'
o.on('-C', '--config PATH', "Hyperion config file (default ./#{DEFAULT_CONFIG_PATH} if it exists)") do |p|
config_path = p
end
o.on('-b', '--bind HOST', 'host (default 127.0.0.1)') { |h| cli_opts[:host] = h }
o.on('-p', '--port PORT', Integer, 'port (default 9292)') { |p| cli_opts[:port] = p }
o.on('-w', '--workers N', Integer, 'worker processes (0 = nprocessors)') { |w| cli_opts[:workers] = w }
o.on('-t', '--threads N', Integer, 'Rack handler thread pool size (0 disables)') do |t|
cli_opts[:thread_count] = t
end
o.on('--tls-cert PATH', 'TLS certificate (PEM)') do |p|
cli_opts[:tls_cert] = OpenSSL::X509::Certificate.new(File.read(p))
end
o.on('--tls-key PATH', 'TLS private key (PEM)') do |p|
cli_opts[:tls_key] = OpenSSL::PKey.read(File.read(p))
end
o.on('--log-level LEVEL', %w[debug info warn error fatal], 'log level (default info)') do |l|
cli_opts[:log_level] = l.to_sym
end
o.on('--log-format FORMAT', %w[text json auto],
'log format: text | json | auto (default auto: json on RAILS_ENV/RACK_ENV=production, colored text on TTY, json otherwise)') do |f|
cli_opts[:log_format] = f.to_sym
end
o.on('--[no-]log-requests',
'Per-request access log line (default ON; pass --no-log-requests to disable).') do |v|
cli_opts[:log_requests] = v
end
o.on('--fiber-local-shim', 'Patch Thread.current[] to be fiber-local (Rails-compat for older gems)') do
cli_opts[:fiber_local_shim] = true
end
o.on('-h', '--help', 'show help') do
puts o
exit 0
end
end
parser.parse!(argv)
config_path ||= DEFAULT_CONFIG_PATH if File.exist?(DEFAULT_CONFIG_PATH)
config = config_path ? Hyperion::Config.load(config_path) : Hyperion::Config.new
config.merge_cli!(cli_opts)
if config.log_level || config.log_format
Hyperion.logger = Hyperion::Logger.new(level: config.log_level, format: config.log_format)
end
Hyperion.log_requests = config.log_requests unless config.log_requests.nil?
rackup = argv.first || 'config.ru'
abort("[hyperion] no such rackup file: #{rackup}") unless File.exist?(rackup)
if config.fiber_local_shim
Hyperion::FiberLocal.install!
Hyperion.logger.info { { message: 'FiberLocal shim installed' } }
end
app = load_rack_app(rackup)
workers = config.workers.zero? ? Etc.nprocessors : config.workers
if workers <= 1
run_single(config, app)
else
run_cluster(config, app, workers)
end
end
|