2016-01-13 20:22:34 +00:00
|
|
|
require "option_parser"
|
|
|
|
|
|
|
|
module Kemal
|
2016-04-18 18:40:48 +00:00
|
|
|
# Handles all the initialization from the command line.
|
2016-01-13 20:22:34 +00:00
|
|
|
class CLI
|
2018-11-30 16:16:08 +00:00
|
|
|
def initialize(args)
|
2016-01-15 12:50:11 +00:00
|
|
|
@ssl_enabled = false
|
2016-04-12 11:41:09 +00:00
|
|
|
@key_file = ""
|
|
|
|
@cert_file = ""
|
2016-01-15 12:50:11 +00:00
|
|
|
@config = Kemal.config
|
2018-11-30 16:16:08 +00:00
|
|
|
if args
|
|
|
|
parse args
|
|
|
|
end
|
2016-01-15 12:50:11 +00:00
|
|
|
configure_ssl
|
|
|
|
end
|
|
|
|
|
2018-11-30 16:16:08 +00:00
|
|
|
private def parse(args : Array(String))
|
|
|
|
OptionParser.parse args do |opts|
|
2016-02-10 19:58:32 +00:00
|
|
|
opts.on("-b HOST", "--bind HOST", "Host to bind (defaults to 0.0.0.0)") do |host_binding|
|
2016-01-15 12:50:11 +00:00
|
|
|
@config.host_binding = host_binding
|
2016-01-13 20:22:34 +00:00
|
|
|
end
|
2016-02-10 19:58:32 +00:00
|
|
|
opts.on("-p PORT", "--port PORT", "Port to listen for connections (defaults to 3000)") do |opt_port|
|
2016-01-15 12:50:11 +00:00
|
|
|
@config.port = opt_port.to_i
|
2016-01-13 20:22:34 +00:00
|
|
|
end
|
|
|
|
opts.on("-s", "--ssl", "Enables SSL") do
|
2016-01-15 12:50:11 +00:00
|
|
|
@ssl_enabled = true
|
2016-01-13 20:22:34 +00:00
|
|
|
end
|
|
|
|
opts.on("--ssl-key-file FILE", "SSL key file") do |key_file|
|
2016-01-15 12:50:11 +00:00
|
|
|
@key_file = key_file
|
2016-01-13 20:22:34 +00:00
|
|
|
end
|
|
|
|
opts.on("--ssl-cert-file FILE", "SSL certificate file") do |cert_file|
|
2016-01-15 12:50:11 +00:00
|
|
|
@cert_file = cert_file
|
2016-01-13 20:22:34 +00:00
|
|
|
end
|
|
|
|
opts.on("-h", "--help", "Shows this help") do
|
|
|
|
puts opts
|
|
|
|
exit 0
|
|
|
|
end
|
2016-07-04 17:23:16 +00:00
|
|
|
@config.extra_options.try &.call(opts)
|
2016-01-13 20:22:34 +00:00
|
|
|
end
|
2016-01-15 12:50:11 +00:00
|
|
|
end
|
2016-01-13 20:22:34 +00:00
|
|
|
|
2017-02-13 20:39:40 +00:00
|
|
|
private def configure_ssl
|
2016-10-01 15:18:28 +00:00
|
|
|
{% if !flag?(:without_openssl) %}
|
2019-08-30 11:20:38 +00:00
|
|
|
if @ssl_enabled
|
|
|
|
abort "SSL Key Not Found" if !@key_file
|
|
|
|
abort "SSL Certificate Not Found" if !@cert_file
|
|
|
|
ssl = Kemal::SSL.new
|
|
|
|
ssl.key_file = @key_file.not_nil!
|
|
|
|
ssl.cert_file = @cert_file.not_nil!
|
|
|
|
Kemal.config.ssl = ssl.context
|
|
|
|
end
|
|
|
|
{% end %}
|
2016-07-03 20:49:16 +00:00
|
|
|
end
|
2016-01-13 20:22:34 +00:00
|
|
|
end
|
|
|
|
end
|