From fd904cd98d8dff92752b3484f6d7714477ddc6dd Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Sat, 19 Mar 2016 15:15:25 +0200 Subject: [PATCH] 0.11.0 --- src/kemal.cr | 54 +++++++++++++++++++++++++-------------------- src/kemal/config.cr | 4 +++- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/kemal.cr b/src/kemal.cr index db3ea3b..fca69d1 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -1,34 +1,40 @@ require "./kemal/*" require "./kemal/middleware/*" -at_exit do - Kemal::CLI.new - config = Kemal.config - config.setup - config.add_handler Kemal::RouteHandler::INSTANCE +module Kemal + def self.run + Kemal::CLI.new + config = Kemal.config + config.setup + config.add_handler Kemal::RouteHandler::INSTANCE - server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers) - server.ssl = config.ssl + server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers) + server.ssl = config.ssl - Signal::INT.trap { - config.logger.write "Kemal is going to take a rest!\n" - server.close - exit - } + Signal::INT.trap { + config.logger.write "Kemal is going to take a rest!\n" + server.close + exit + } - # This route serves the built-in images for not_found and exceptions. - get "/__kemal__/:image" do |env| - image = env.params.url["image"] - file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current) - if File.exists? file_path - env.response.headers.add "Content-Type", "application/octet-stream" - env.response.content_length = File.size(file_path) - File.open(file_path) do |file| - IO.copy(file, env.response) + # This route serves the built-in images for not_found and exceptions. + get "/__kemal__/:image" do |env| + image = env.params.url["image"] + file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current) + if File.exists? file_path + env.response.headers.add "Content-Type", "application/octet-stream" + env.response.content_length = File.size(file_path) + File.open(file_path) do |file| + IO.copy(file, env.response) + end end end - end - config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n" - server.listen + config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n" + server.listen + end +end + +at_exit do + Kemal.run if Kemal.config.run end diff --git a/src/kemal/config.cr b/src/kemal/config.cr index 83da0d6..f9b6b6b 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -2,7 +2,8 @@ module Kemal class Config INSTANCE = Config.new HANDLERS = [] of HTTP::Handler - property host_binding, ssl, port, env, public_folder, logging, always_rescue, error_handler, serve_static + property host_binding, ssl, port, env, public_folder, logging, + always_rescue, error_handler, serve_static, run def initialize @host_binding = "0.0.0.0" @@ -14,6 +15,7 @@ module Kemal @logger = nil @always_rescue = true @error_handler = nil + @run = false end def logger