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
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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -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 %}

View file

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

View file

@ -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

View file

@ -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

View file

@ -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