From 467a1b4581b663a97ad641162b90572636d422e1 Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Wed, 3 Feb 2016 22:08:54 +0200 Subject: [PATCH] Rename handlers --- spec/context_spec.cr | 8 ++--- spec/logger_spec.cr | 10 +++--- ..._handler_spec.cr => route_handler_spec.cr} | 32 +++++++++---------- spec/route_spec.cr | 2 +- spec/view_spec.cr | 6 ++-- ...dler_spec.cr => websocket_handler_spec.cr} | 0 src/kemal.cr | 4 +-- src/kemal/dsl.cr | 2 +- src/kemal/{logger.cr => log_handler.cr} | 3 +- src/kemal/macros.cr | 2 +- src/kemal/param_parser.cr | 5 ++- src/kemal/{handler.cr => route_handler.cr} | 6 ++-- 12 files changed, 40 insertions(+), 40 deletions(-) rename spec/{kemal_handler_spec.cr => route_handler_spec.cr} (92%) rename spec/{kemal_ws_handler_spec.cr => websocket_handler_spec.cr} (100%) rename src/kemal/{logger.cr => log_handler.cr} (97%) rename src/kemal/{handler.cr => route_handler.cr} (86%) diff --git a/spec/context_spec.cr b/spec/context_spec.cr index 87c755b..b2dd5ea 100644 --- a/spec/context_spec.cr +++ b/spec/context_spec.cr @@ -2,7 +2,7 @@ require "./spec_helper" describe "Context" do it "has a default content type" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/" do |env| "Hello" end @@ -13,7 +13,7 @@ describe "Context" do end it "sets content type" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/" do |env| env.response.content_type = "application/json" "Hello" @@ -25,7 +25,7 @@ describe "Context" do end it "parses headers" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/" do |env| name = env.request.headers["name"] "Hello #{name}" @@ -39,7 +39,7 @@ describe "Context" do end it "sets response headers" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/" do |env| env.response.headers.add "Accept-Language", "tr" end diff --git a/spec/logger_spec.cr b/spec/logger_spec.cr index fb11784..e5af632 100644 --- a/spec/logger_spec.cr +++ b/spec/logger_spec.cr @@ -1,28 +1,28 @@ require "./spec_helper" -describe "Logger" do +describe "Kemal::LogHandler" do it "creates a handler" do - logger = Kemal::Logger.new + logger = Kemal::LogHandler.new logger.handler.should_not be nil end it "creates a STDOUT handler by default" do config = Kemal.config - logger = Kemal::Logger.new + logger = Kemal::LogHandler.new logger.handler.should be_a IO end it "creates a file handler in production" do config = Kemal.config config.env = "production" - logger = Kemal::Logger.new + logger = Kemal::LogHandler.new logger.handler.should be_a File end it "writes to a file in production" do config = Kemal.config config.env = "production" - logger = Kemal::Logger.new + logger = Kemal::LogHandler.new request = HTTP::Request.new("GET", "/?message=world&time=now") io = MemoryIO.new response = HTTP::Server::Response.new(io) diff --git a/spec/kemal_handler_spec.cr b/spec/route_handler_spec.cr similarity index 92% rename from spec/kemal_handler_spec.cr rename to spec/route_handler_spec.cr index c18f8ac..f941b9c 100644 --- a/spec/kemal_handler_spec.cr +++ b/spec/route_handler_spec.cr @@ -1,8 +1,8 @@ require "./spec_helper" -describe "Kemal::Handler" do +describe "Kemal::RouteHandler" do it "routes" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/" do "hello" end @@ -13,7 +13,7 @@ describe "Kemal::Handler" do end it "routes request with query string" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/" do |env| "hello #{env.params["message"]}" end @@ -24,7 +24,7 @@ describe "Kemal::Handler" do end it "routes request with multiple query strings" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/" do |env| "hello #{env.params["message"]} time #{env.params["time"]}" end @@ -35,7 +35,7 @@ describe "Kemal::Handler" do end it "route parameter has more precedence than query string arguments" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/:message" do |env| "hello #{env.params["message"]}" end @@ -46,7 +46,7 @@ describe "Kemal::Handler" do end it "parses simple JSON body" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "POST", "/" do |env| name = env.params["name"] age = env.params["age"] @@ -66,7 +66,7 @@ describe "Kemal::Handler" do end it "parses JSON with string array" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "POST", "/" do |env| skills = env.params["skills"] as Array "Skills #{skills.each.join(',')}" @@ -85,7 +85,7 @@ describe "Kemal::Handler" do end it "parses JSON with json object array" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "POST", "/" do |env| skills = env.params["skills"] as Array skills_from_languages = skills.map do |skill| @@ -109,7 +109,7 @@ describe "Kemal::Handler" do end it "renders 404 on not found" do - kemal = Kemal::Handler.new + 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) @@ -117,7 +117,7 @@ describe "Kemal::Handler" do end # it "renders 500 on exception" do - # kemal = Kemal::Handler.new + # kemal = Kemal::RouteHandler.new # kemal.add_route "GET", "/" do # raise "Exception" # end @@ -129,7 +129,7 @@ describe "Kemal::Handler" do # end # it "checks for _method param in POST request to simulate PUT" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "PUT", "/" do |env| "Hello World from PUT" end @@ -145,7 +145,7 @@ describe "Kemal::Handler" do end it "checks for _method param in POST request to simulate PATCH" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "PATCH", "/" do |env| "Hello World from PATCH" end @@ -161,7 +161,7 @@ describe "Kemal::Handler" do end it "checks for _method param in POST request to simulate DELETE" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "DELETE", "/" do |env| "Hello World from DELETE" end @@ -178,7 +178,7 @@ describe "Kemal::Handler" do end it "can process HTTP HEAD requests for defined GET routes" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/" do |env| "Hello World from GET" end @@ -189,7 +189,7 @@ describe "Kemal::Handler" do end it "can't process HTTP HEAD requests for undefined GET routes" do - kemal = Kemal::Handler.new + 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) @@ -197,7 +197,7 @@ describe "Kemal::Handler" do end it "redirects user to provided url" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/" do |env| env.redirect "/login" end diff --git a/spec/route_spec.cr b/spec/route_spec.cr index 335e301..fc83cf8 100644 --- a/spec/route_spec.cr +++ b/spec/route_spec.cr @@ -3,7 +3,7 @@ require "./spec_helper" describe "Route" do describe "match?" do it "matches the correct route" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/route1" do |env| "Route 1" end diff --git a/spec/view_spec.cr b/spec/view_spec.cr index 23698e4..8dc38a8 100644 --- a/spec/view_spec.cr +++ b/spec/view_spec.cr @@ -6,7 +6,7 @@ end describe "Views" do it "renders file" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/view/:name" do |env| name = env.params["name"] render "spec/asset/hello.ecr" @@ -18,7 +18,7 @@ describe "Views" do end it "renders file with dynamic variables" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/view/:name" do |env| name = env.params["name"] render_with_base_and_layout "hello.ecr" @@ -30,7 +30,7 @@ describe "Views" do end it "renders layout" do - kemal = Kemal::Handler.new + kemal = Kemal::RouteHandler.new kemal.add_route "GET", "/view/:name" do |env| name = env.params["name"] render "spec/asset/hello.ecr", "spec/asset/layout.ecr" diff --git a/spec/kemal_ws_handler_spec.cr b/spec/websocket_handler_spec.cr similarity index 100% rename from spec/kemal_ws_handler_spec.cr rename to spec/websocket_handler_spec.cr diff --git a/src/kemal.cr b/src/kemal.cr index d084d6d..8f94fc2 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -5,12 +5,12 @@ at_exit do Kemal::CLI.new config = Kemal.config if config.logging - logger = Kemal::Logger.new + logger = Kemal::LogHandler.new config.logger = logger config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n" end config.add_handler Kemal::StaticFileHandler.new(config.public_folder) - config.add_handler Kemal::Handler::INSTANCE + 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 diff --git a/src/kemal/dsl.cr b/src/kemal/dsl.cr index 73d9262..b9673b2 100644 --- a/src/kemal/dsl.cr +++ b/src/kemal/dsl.cr @@ -2,7 +2,7 @@ HTTP_METHODS = %w(get post put patch delete) {% for method in HTTP_METHODS %} def {{method.id}}(path, &block : HTTP::Server::Context -> _) - Kemal::Handler::INSTANCE.add_route({{method}}.upcase, path, &block) + Kemal::RouteHandler::INSTANCE.add_route({{method}}.upcase, path, &block) end {% end %} diff --git a/src/kemal/logger.cr b/src/kemal/log_handler.cr similarity index 97% rename from src/kemal/logger.cr rename to src/kemal/log_handler.cr index 7413239..7e76265 100644 --- a/src/kemal/logger.cr +++ b/src/kemal/log_handler.cr @@ -1,6 +1,7 @@ require "colorize" +require "http" -class Kemal::Logger < HTTP::Handler +class Kemal::LogHandler < HTTP::Handler INSTANCE = new getter handler diff --git a/src/kemal/macros.cr b/src/kemal/macros.cr index 8bd079e..025f74e 100644 --- a/src/kemal/macros.cr +++ b/src/kemal/macros.cr @@ -34,7 +34,7 @@ end # development: STDOUT in # production: kemal.log macro log(message) - Kemal::Logger::INSTANCE.write "#{{{message}}}\n" if Kemal.config.logging + Kemal::LogHandler::INSTANCE.write "#{{{message}}}\n" if Kemal.config.logging end # Enables / Disables logging diff --git a/src/kemal/param_parser.cr b/src/kemal/param_parser.cr index 9c6544d..9cd957a 100644 --- a/src/kemal/param_parser.cr +++ b/src/kemal/param_parser.cr @@ -36,9 +36,8 @@ class Kemal::ParamParser end def parse_url_params - params = @request.url_params - if params - params.not_nil!.each do |key, value| + if params = @request.url_params + params.each do |key, value| @params[key] = value end end diff --git a/src/kemal/handler.cr b/src/kemal/route_handler.cr similarity index 86% rename from src/kemal/handler.cr rename to src/kemal/route_handler.cr index 3ac918c..e180762 100644 --- a/src/kemal/handler.cr +++ b/src/kemal/route_handler.cr @@ -1,10 +1,10 @@ require "http/server" require "radix" -# Kemal::Handler is the main handler which handles all the HTTP requests. Routing, parsing, rendering e.g +# Kemal::RouteHandler is the main handler which handles all the HTTP requests. Routing, parsing, rendering e.g # are done in this handler. -class Kemal::Handler < HTTP::Handler +class Kemal::RouteHandler < HTTP::Handler INSTANCE = new def initialize @@ -36,7 +36,7 @@ class Kemal::Handler < HTTP::Handler context.response.print body return context rescue ex - Kemal::Logger::INSTANCE.write "Exception: #{ex.to_s}\n" + Kemal::LogHandler::INSTANCE.write "Exception: #{ex.to_s}\n" return render_500(context, ex.to_s) end end