Update CLI to handle missing ssl_key and ssl_cert

This commit is contained in:
sdogruyol 2016-01-15 14:50:11 +02:00
parent 3e27e15e29
commit 7ffa6700de
2 changed files with 25 additions and 10 deletions

View File

@ -3,9 +3,7 @@ require "./kemal/middleware/*"
at_exit do
Kemal::CLI.new
config = Kemal.config
logger = Kemal::Logger.new
config.add_handler logger
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)

View File

@ -3,33 +3,50 @@ require "option_parser"
module Kemal
class CLI
def initialize
ssl = nil
@ssl_enabled = false
@key_file = nil
@cert_file = nil
@config = Kemal.config
parse
configure_ssl
end
def parse
OptionParser.parse! do |opts|
opts.on("-b HOST", "--bind HOST", "HTTP host to bind (defaults to 0.0.0.0)") do |host_binding|
Kemal.config.host_binding = host_binding
@config.host_binding = host_binding
end
opts.on("-p PORT", "--port PORT", "HTTP port to listen connections (defaults to 3000)") do |opt_port|
Kemal.config.port = opt_port.to_i
@config.port = opt_port.to_i
end
opts.on("-e ENV", "--environment ENV", "Running environment [development, production] (defaults to development). Set `production` to boost performance") do |env|
Kemal.config.env = env
@config.env = env
end
opts.on("-s", "--ssl", "Enables SSL") do
ssl = Kemal::Middleware::SSL.new
@ssl_enabled = true
end
opts.on("--ssl-key-file FILE", "SSL key file") do |key_file|
ssl.not_nil!.set_key_file key_file
@key_file = key_file
end
opts.on("--ssl-cert-file FILE", "SSL certificate file") do |cert_file|
ssl.not_nil!.set_cert_file cert_file
@cert_file = cert_file
end
opts.on("-h", "--help", "Shows this help") do
puts opts
exit 0
end
end
end
Kemal.config.ssl = ssl.not_nil!.context if ssl
def configure_ssl
if @ssl_enabled
puts "SSL Key Not Found"; exit unless @key_file
puts "SSL Certificate Not Found"; exit unless @cert_file
ssl = Kemal::Middleware::SSL.new
ssl.set_key_file @key_file.not_nil!.to_slice
ssl.set_cert_file @cert_file.not_nil!.to_slice
Kemal.config.ssl = ssl.context
end
end
end
end