From 092f5a2e4948252316f3070440c4799671cbb97f Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Sun, 14 Feb 2016 15:15:28 +0200 Subject: [PATCH] Improve kemal.cr --- spec/config_spec.cr | 1 - spec/macros_spec.cr | 1 - spec/route_handler_spec.cr | 8 ++++---- src/kemal.cr | 6 +++--- src/kemal/base_log_handler.cr | 1 - src/kemal/common_error_handler.cr | 1 - src/kemal/common_log_handler.cr | 3 +-- src/kemal/config.cr | 1 - src/kemal/exceptions.cr | 2 -- src/kemal/macros.cr | 7 +++---- src/kemal/middleware/filters.cr | 3 +-- src/kemal/null_log_handler.cr | 2 +- src/kemal/route_handler.cr | 1 - 13 files changed, 13 insertions(+), 24 deletions(-) diff --git a/spec/config_spec.cr b/spec/config_spec.cr index 8fd88d2..56441d4 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -28,5 +28,4 @@ describe "Config" do config.add_handler CustomTestHandler.new config.handlers.size.should eq(1) end - end diff --git a/spec/macros_spec.cr b/spec/macros_spec.cr index 00d4ab2..5b79983 100644 --- a/spec/macros_spec.cr +++ b/spec/macros_spec.cr @@ -27,7 +27,6 @@ describe "Macros" do logging false Kemal.config.logging.should eq false end - it "sets a custom logger" do config = Kemal::Config::INSTANCE logger CustomLogHandler.new("production") diff --git a/spec/route_handler_spec.cr b/spec/route_handler_spec.cr index 96c3b24..4afb393 100644 --- a/spec/route_handler_spec.cr +++ b/spec/route_handler_spec.cr @@ -109,13 +109,13 @@ describe "Kemal::RouteHandler" do end # Removed until there is a way to test multiple middlewares - #it "renders 404 on not found" do + # it "renders 404 on not found" do # kemal = Kemal::RouteHandler.new # request = HTTP::Request.new("GET", "/?message=world") # io_with_context = create_request_and_return_io(kemal, request) # client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) # client_response.status_code.should eq 404 - #end + # end # it "renders 500 on exception" do # kemal = Kemal::RouteHandler.new @@ -190,13 +190,13 @@ describe "Kemal::RouteHandler" do end # Removed until there is a way to test multiple middlewares - #it "can't process HTTP HEAD requests for undefined GET routes" do + # it "can't process HTTP HEAD requests for undefined GET routes" do # kemal = Kemal::RouteHandler.new # request = HTTP::Request.new("HEAD", "/") # io_with_context = create_request_and_return_io(kemal, request) # client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) # client_response.status_code.should eq(404) - #end + # end it "redirects user to provided url" do kemal = Kemal::RouteHandler.new diff --git a/src/kemal.cr b/src/kemal.cr index 7798e9e..ada481b 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -5,9 +5,8 @@ at_exit do Kemal::CLI.new config = Kemal.config config.setup_logging - config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n" - config.add_handler Kemal::StaticFileHandler.new(config.public_folder) config.setup_error_handler + config.add_handler Kemal::StaticFileHandler.new(config.public_folder) config.add_handler Kemal::RouteHandler::INSTANCE server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers) @@ -31,6 +30,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/base_log_handler.cr b/src/kemal/base_log_handler.cr index 71b72f9..3495e40 100644 --- a/src/kemal/base_log_handler.cr +++ b/src/kemal/base_log_handler.cr @@ -1,7 +1,6 @@ require "http" class Kemal::BaseLogHandler < HTTP::Handler - def initialize(@env) end diff --git a/src/kemal/common_error_handler.cr b/src/kemal/common_error_handler.cr index f3fbc4d..b3666e7 100644 --- a/src/kemal/common_error_handler.cr +++ b/src/kemal/common_error_handler.cr @@ -12,5 +12,4 @@ class Kemal::CommonErrorHandler < HTTP::Handler return render_500(context, ex.to_s) end end - end diff --git a/src/kemal/common_log_handler.cr b/src/kemal/common_log_handler.cr index dbb858f..ab03eb3 100644 --- a/src/kemal/common_log_handler.cr +++ b/src/kemal/common_log_handler.cr @@ -1,8 +1,7 @@ require "colorize" require "http" -class Kemal::CommonLogHandler < Kemal::BaseLogHandler - # INSTANCE = new +class Kemal::CommonLogHandler < Kemal::BaseLogHandler getter handler def initialize(@env) diff --git a/src/kemal/config.cr b/src/kemal/config.cr index e3452d3..498f58d 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -55,7 +55,6 @@ module Kemal HANDLERS << @error_handler.not_nil! end end - end def self.config diff --git a/src/kemal/exceptions.cr b/src/kemal/exceptions.cr index 9a087b9..b7d7275 100644 --- a/src/kemal/exceptions.cr +++ b/src/kemal/exceptions.cr @@ -1,9 +1,7 @@ module Kemal::Exceptions - class RouteNotFound < Exception def initialize(context) super "Requested path: '#{context.request.override_method as String}:#{context.request.path}' was not found." end end - end diff --git a/src/kemal/macros.cr b/src/kemal/macros.cr index 0abcf6c..b71bc18 100644 --- a/src/kemal/macros.cr +++ b/src/kemal/macros.cr @@ -40,11 +40,10 @@ end # Enables / Disables logging macro logging(status) Kemal.config.logging = {{status}} -end +end + +macro logger(logger) -macro logger(logger) Kemal.config.logger = {{logger}} Kemal.config.add_handler {{logger}} end - - diff --git a/src/kemal/middleware/filters.cr b/src/kemal/middleware/filters.cr index a0e0c2d..9a49ec6 100644 --- a/src/kemal/middleware/filters.cr +++ b/src/kemal/middleware/filters.cr @@ -45,15 +45,14 @@ module Kemal::Middleware super "An after-filter is already defined for path: '#{path}'." end end - end class Block property block + def initialize(&@block : HTTP::Server::Context -> _) end end - end def add_filters diff --git a/src/kemal/null_log_handler.cr b/src/kemal/null_log_handler.cr index 5eec3ef..07d9665 100644 --- a/src/kemal/null_log_handler.cr +++ b/src/kemal/null_log_handler.cr @@ -1,2 +1,2 @@ -class Kemal::NullLogHandler < Kemal::BaseLogHandler +class Kemal::NullLogHandler < Kemal::BaseLogHandler end diff --git a/src/kemal/route_handler.cr b/src/kemal/route_handler.cr index 62f9c1d..74fa6ea 100644 --- a/src/kemal/route_handler.cr +++ b/src/kemal/route_handler.cr @@ -41,5 +41,4 @@ class Kemal::RouteHandler < HTTP::Handler node = radix_path method, path @tree.add node, route end - end