Rename handlers

This commit is contained in:
Sdogruyol 2016-02-03 22:08:54 +02:00
parent a4005e98be
commit 467a1b4581
12 changed files with 40 additions and 40 deletions

View file

@ -2,7 +2,7 @@ require "./spec_helper"
describe "Context" do describe "Context" do
it "has a default content type" do it "has a default content type" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/" do |env| kemal.add_route "GET", "/" do |env|
"Hello" "Hello"
end end
@ -13,7 +13,7 @@ describe "Context" do
end end
it "sets content type" do it "sets content type" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/" do |env| kemal.add_route "GET", "/" do |env|
env.response.content_type = "application/json" env.response.content_type = "application/json"
"Hello" "Hello"
@ -25,7 +25,7 @@ describe "Context" do
end end
it "parses headers" do it "parses headers" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/" do |env| kemal.add_route "GET", "/" do |env|
name = env.request.headers["name"] name = env.request.headers["name"]
"Hello #{name}" "Hello #{name}"
@ -39,7 +39,7 @@ describe "Context" do
end end
it "sets response headers" do it "sets response headers" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/" do |env| kemal.add_route "GET", "/" do |env|
env.response.headers.add "Accept-Language", "tr" env.response.headers.add "Accept-Language", "tr"
end end

View file

@ -1,28 +1,28 @@
require "./spec_helper" require "./spec_helper"
describe "Logger" do describe "Kemal::LogHandler" do
it "creates a handler" do it "creates a handler" do
logger = Kemal::Logger.new logger = Kemal::LogHandler.new
logger.handler.should_not be nil logger.handler.should_not be nil
end end
it "creates a STDOUT handler by default" do it "creates a STDOUT handler by default" do
config = Kemal.config config = Kemal.config
logger = Kemal::Logger.new logger = Kemal::LogHandler.new
logger.handler.should be_a IO logger.handler.should be_a IO
end end
it "creates a file handler in production" do it "creates a file handler in production" do
config = Kemal.config config = Kemal.config
config.env = "production" config.env = "production"
logger = Kemal::Logger.new logger = Kemal::LogHandler.new
logger.handler.should be_a File logger.handler.should be_a File
end end
it "writes to a file in production" do it "writes to a file in production" do
config = Kemal.config config = Kemal.config
config.env = "production" config.env = "production"
logger = Kemal::Logger.new logger = Kemal::LogHandler.new
request = HTTP::Request.new("GET", "/?message=world&time=now") request = HTTP::Request.new("GET", "/?message=world&time=now")
io = MemoryIO.new io = MemoryIO.new
response = HTTP::Server::Response.new(io) response = HTTP::Server::Response.new(io)

View file

@ -1,8 +1,8 @@
require "./spec_helper" require "./spec_helper"
describe "Kemal::Handler" do describe "Kemal::RouteHandler" do
it "routes" do it "routes" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/" do kemal.add_route "GET", "/" do
"hello" "hello"
end end
@ -13,7 +13,7 @@ describe "Kemal::Handler" do
end end
it "routes request with query string" do it "routes request with query string" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/" do |env| kemal.add_route "GET", "/" do |env|
"hello #{env.params["message"]}" "hello #{env.params["message"]}"
end end
@ -24,7 +24,7 @@ describe "Kemal::Handler" do
end end
it "routes request with multiple query strings" do it "routes request with multiple query strings" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/" do |env| kemal.add_route "GET", "/" do |env|
"hello #{env.params["message"]} time #{env.params["time"]}" "hello #{env.params["message"]} time #{env.params["time"]}"
end end
@ -35,7 +35,7 @@ describe "Kemal::Handler" do
end end
it "route parameter has more precedence than query string arguments" do 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| kemal.add_route "GET", "/:message" do |env|
"hello #{env.params["message"]}" "hello #{env.params["message"]}"
end end
@ -46,7 +46,7 @@ describe "Kemal::Handler" do
end end
it "parses simple JSON body" do it "parses simple JSON body" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "POST", "/" do |env| kemal.add_route "POST", "/" do |env|
name = env.params["name"] name = env.params["name"]
age = env.params["age"] age = env.params["age"]
@ -66,7 +66,7 @@ describe "Kemal::Handler" do
end end
it "parses JSON with string array" do it "parses JSON with string array" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "POST", "/" do |env| kemal.add_route "POST", "/" do |env|
skills = env.params["skills"] as Array skills = env.params["skills"] as Array
"Skills #{skills.each.join(',')}" "Skills #{skills.each.join(',')}"
@ -85,7 +85,7 @@ describe "Kemal::Handler" do
end end
it "parses JSON with json object array" do it "parses JSON with json object array" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "POST", "/" do |env| kemal.add_route "POST", "/" do |env|
skills = env.params["skills"] as Array skills = env.params["skills"] as Array
skills_from_languages = skills.map do |skill| skills_from_languages = skills.map do |skill|
@ -109,7 +109,7 @@ describe "Kemal::Handler" do
end end
it "renders 404 on not found" do it "renders 404 on not found" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
request = HTTP::Request.new("GET", "/?message=world") request = HTTP::Request.new("GET", "/?message=world")
io_with_context = create_request_and_return_io(kemal, request) io_with_context = create_request_and_return_io(kemal, request)
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
@ -117,7 +117,7 @@ describe "Kemal::Handler" do
end end
# it "renders 500 on exception" do # it "renders 500 on exception" do
# kemal = Kemal::Handler.new # kemal = Kemal::RouteHandler.new
# kemal.add_route "GET", "/" do # kemal.add_route "GET", "/" do
# raise "Exception" # raise "Exception"
# end # end
@ -129,7 +129,7 @@ describe "Kemal::Handler" do
# end # end
# #
it "checks for _method param in POST request to simulate PUT" do 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| kemal.add_route "PUT", "/" do |env|
"Hello World from PUT" "Hello World from PUT"
end end
@ -145,7 +145,7 @@ describe "Kemal::Handler" do
end end
it "checks for _method param in POST request to simulate PATCH" do 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| kemal.add_route "PATCH", "/" do |env|
"Hello World from PATCH" "Hello World from PATCH"
end end
@ -161,7 +161,7 @@ describe "Kemal::Handler" do
end end
it "checks for _method param in POST request to simulate DELETE" do 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| kemal.add_route "DELETE", "/" do |env|
"Hello World from DELETE" "Hello World from DELETE"
end end
@ -178,7 +178,7 @@ describe "Kemal::Handler" do
end end
it "can process HTTP HEAD requests for defined GET routes" do 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| kemal.add_route "GET", "/" do |env|
"Hello World from GET" "Hello World from GET"
end end
@ -189,7 +189,7 @@ describe "Kemal::Handler" do
end end
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::Handler.new kemal = Kemal::RouteHandler.new
request = HTTP::Request.new("HEAD", "/") request = HTTP::Request.new("HEAD", "/")
io_with_context = create_request_and_return_io(kemal, request) io_with_context = create_request_and_return_io(kemal, request)
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
@ -197,7 +197,7 @@ describe "Kemal::Handler" do
end end
it "redirects user to provided url" do it "redirects user to provided url" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/" do |env| kemal.add_route "GET", "/" do |env|
env.redirect "/login" env.redirect "/login"
end end

View file

@ -3,7 +3,7 @@ require "./spec_helper"
describe "Route" do describe "Route" do
describe "match?" do describe "match?" do
it "matches the correct route" do it "matches the correct route" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/route1" do |env| kemal.add_route "GET", "/route1" do |env|
"Route 1" "Route 1"
end end

View file

@ -6,7 +6,7 @@ end
describe "Views" do describe "Views" do
it "renders file" do it "renders file" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/view/:name" do |env| kemal.add_route "GET", "/view/:name" do |env|
name = env.params["name"] name = env.params["name"]
render "spec/asset/hello.ecr" render "spec/asset/hello.ecr"
@ -18,7 +18,7 @@ describe "Views" do
end end
it "renders file with dynamic variables" do it "renders file with dynamic variables" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/view/:name" do |env| kemal.add_route "GET", "/view/:name" do |env|
name = env.params["name"] name = env.params["name"]
render_with_base_and_layout "hello.ecr" render_with_base_and_layout "hello.ecr"
@ -30,7 +30,7 @@ describe "Views" do
end end
it "renders layout" do it "renders layout" do
kemal = Kemal::Handler.new kemal = Kemal::RouteHandler.new
kemal.add_route "GET", "/view/:name" do |env| kemal.add_route "GET", "/view/:name" do |env|
name = env.params["name"] name = env.params["name"]
render "spec/asset/hello.ecr", "spec/asset/layout.ecr" render "spec/asset/hello.ecr", "spec/asset/layout.ecr"

View file

@ -5,12 +5,12 @@ at_exit do
Kemal::CLI.new Kemal::CLI.new
config = Kemal.config config = Kemal.config
if config.logging if config.logging
logger = Kemal::Logger.new logger = Kemal::LogHandler.new
config.logger = logger config.logger = logger
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n" config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
end end
config.add_handler Kemal::StaticFileHandler.new(config.public_folder) 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 = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers)
server.ssl = config.ssl server.ssl = config.ssl

View file

@ -2,7 +2,7 @@ HTTP_METHODS = %w(get post put patch delete)
{% for method in HTTP_METHODS %} {% for method in HTTP_METHODS %}
def {{method.id}}(path, &block : HTTP::Server::Context -> _) 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
{% end %} {% end %}

View file

@ -1,6 +1,7 @@
require "colorize" require "colorize"
require "http"
class Kemal::Logger < HTTP::Handler class Kemal::LogHandler < HTTP::Handler
INSTANCE = new INSTANCE = new
getter handler getter handler

View file

@ -34,7 +34,7 @@ end
# development: STDOUT in # development: STDOUT in
# production: kemal.log # production: kemal.log
macro log(message) macro log(message)
Kemal::Logger::INSTANCE.write "#{{{message}}}\n" if Kemal.config.logging Kemal::LogHandler::INSTANCE.write "#{{{message}}}\n" if Kemal.config.logging
end end
# Enables / Disables logging # Enables / Disables logging

View file

@ -36,9 +36,8 @@ class Kemal::ParamParser
end end
def parse_url_params def parse_url_params
params = @request.url_params if params = @request.url_params
if params params.each do |key, value|
params.not_nil!.each do |key, value|
@params[key] = value @params[key] = value
end end
end end

View file

@ -1,10 +1,10 @@
require "http/server" require "http/server"
require "radix" 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. # are done in this handler.
class Kemal::Handler < HTTP::Handler class Kemal::RouteHandler < HTTP::Handler
INSTANCE = new INSTANCE = new
def initialize def initialize
@ -36,7 +36,7 @@ class Kemal::Handler < HTTP::Handler
context.response.print body context.response.print body
return context return context
rescue ex 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) return render_500(context, ex.to_s)
end end
end end