From 7ffa6700de98a2cca47df9c4bbb7af0c42e96095 Mon Sep 17 00:00:00 2001 From: sdogruyol Date: Fri, 15 Jan 2016 14:50:11 +0200 Subject: [PATCH] Update CLI to handle missing ssl_key and ssl_cert --- src/kemal.cr | 2 -- src/kemal/cli.cr | 33 +++++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/kemal.cr b/src/kemal.cr index 9353e7d..5431303 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -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) diff --git a/src/kemal/cli.cr b/src/kemal/cli.cr index 0a710af..d9bd095 100644 --- a/src/kemal/cli.cr +++ b/src/kemal/cli.cr @@ -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