From ed769ad700ff67b79a5c6e31560e02a78f509e39 Mon Sep 17 00:00:00 2001 From: Joris Moriau Date: Mon, 15 Feb 2016 08:01:41 +0100 Subject: [PATCH] added configure serve_static --- src/kemal.cr | 6 ++---- src/kemal/config.cr | 19 +++++++++++++++---- src/kemal/helpers.cr | 4 ++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/kemal.cr b/src/kemal.cr index ada481b..f89976e 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -4,9 +4,7 @@ require "./kemal/middleware/*" at_exit do Kemal::CLI.new config = Kemal.config - config.setup_logging - config.setup_error_handler - config.add_handler Kemal::StaticFileHandler.new(config.public_folder) + config.setup config.add_handler Kemal::RouteHandler::INSTANCE server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers) @@ -30,7 +28,7 @@ at_exit do end end end - + config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n" server.listen end diff --git a/src/kemal/config.cr b/src/kemal/config.cr index 498f58d..1510ec5 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -2,12 +2,13 @@ module Kemal class Config INSTANCE = Config.new HANDLERS = [] of HTTP::Handler - property host_binding, ssl, port, env, public_folder, logging, always_rescue, error_handler + property host_binding, ssl, port, env, public_folder, logging, always_rescue, error_handler, serve_static def initialize - @host_binding = "0.0.0.0" unless @host_binding + @host_binding = "0.0.0.0" @port = 3000 - @env = "development" unless @env + @env = "development" + @serve_static = true @public_folder = "./public" @logging = true @logger = nil @@ -39,6 +40,12 @@ module Kemal HANDLERS << handler end + def setup + setup_logging + setup_error_handler + setup_public_folder + end + def setup_logging if @logging @logger ||= Kemal::CommonLogHandler.new(@env) @@ -49,12 +56,16 @@ module Kemal end end - def setup_error_handler + private def setup_error_handler if @always_rescue @error_handler ||= Kemal::CommonErrorHandler::INSTANCE HANDLERS << @error_handler.not_nil! end end + + private def setup_public_folder + HANDLERS << Kemal::StaticFileHandler.new(@public_folder) if @serve_static + end end def self.config diff --git a/src/kemal/helpers.cr b/src/kemal/helpers.cr index d410195..df67bd8 100644 --- a/src/kemal/helpers.cr +++ b/src/kemal/helpers.cr @@ -46,3 +46,7 @@ def logger(logger) Kemal.config.logger = logger Kemal.config.add_handler logger end + +def serve_static(status) + Kemal.config.serve_static = status +end