Add more types to method signatures

This commit is contained in:
Serdar Dogruyol 2017-08-25 16:41:02 +03:00
parent 00217d9545
commit 79e324efaf
11 changed files with 30 additions and 30 deletions

View file

@ -3,7 +3,7 @@ module Kemal
abstract class BaseLogHandler
include HTTP::Handler
abstract def call(context)
abstract def call(context : HTTP::Server::Context)
abstract def write(message : String)
end
end

View file

@ -4,7 +4,7 @@ module Kemal
include HTTP::Handler
INSTANCE = new
def call(context)
def call(context : HTTP::Server::Context)
begin
call_next(context)
rescue ex : Kemal::Exceptions::RouteNotFound

View file

@ -7,7 +7,7 @@ module Kemal
@handler = io
end
def call(context)
def call(context : HTTP::Server::Context)
time = Time.now
call_next(context)
elapsed_text = elapsed_text(Time.now - time)

View file

@ -1,19 +1,19 @@
# Exceptions for 404 and custom errors are defined here.
module Kemal::Exceptions
class InvalidPathStartException < Exception
def initialize(method, path)
def initialize(method : String, path : String)
super "Route declaration #{method} \"#{path}\" needs to start with '/', should be #{method} \"/#{path}\""
end
end
class RouteNotFound < Exception
def initialize(context)
def initialize(context : HTTP::Server::Context)
super "Requested path: '#{context.request.override_method.as(String)}:#{context.request.path}' was not found."
end
end
class CustomException < Exception
def initialize(context)
def initialize(context : HTTP::Server::Context)
super "Rendered error with #{context.response.status_code}"
end
end

View file

@ -11,7 +11,7 @@ module Kemal
end
# The call order of the filters is before_all -> before_x -> X -> after_x -> after_all
def call(context)
def call(context : HTTP::Server::Context)
return call_next(context) unless context.route_defined?
call_block_for_path_type("ALL", context.request.path, :before, context)
call_block_for_path_type(context.request.override_method, context.request.path, :before, context)
@ -26,7 +26,7 @@ module Kemal
# :nodoc: This shouldn't be called directly, it's not private because I need to call it for testing purpose since I can't call the macros in the spec.
# It adds the block for the corresponding verb/path/type combination to the tree.
def _add_route_filter(verb, path, type, &block : HTTP::Server::Context -> _)
def _add_route_filter(verb : String, path, type, &block : HTTP::Server::Context -> _)
lookup = lookup_filters_for_path_type(verb, path, type)
if lookup.found? && lookup.payload.is_a?(Array(FilterBlock))
(lookup.payload.as(Array(FilterBlock))) << FilterBlock.new(&block)
@ -36,17 +36,17 @@ module Kemal
end
# This can be called directly but it's simpler to just use the macros, it will check if another filter is not already defined for this verb/path/type and proceed to call `add_route_filter`
def before(verb, path = "*", &block : HTTP::Server::Context -> _)
def before(verb : String, path : String = "*", &block : HTTP::Server::Context -> _)
_add_route_filter verb, path, :before, &block
end
# This can be called directly but it's simpler to just use the macros, it will check if another filter is not already defined for this verb/path/type and proceed to call `add_route_filter`
def after(verb, path = "*", &block : HTTP::Server::Context -> _)
def after(verb : String, path : String = "*", &block : HTTP::Server::Context -> _)
_add_route_filter verb, path, :after, &block
end
# This will fetch the block for the verb/path/type from the tree and call it.
private def call_block_for_path_type(verb, path, type, context)
private def call_block_for_path_type(verb : String?, path : String, type, context : HTTP::Server::Context)
lookup = lookup_filters_for_path_type(verb, path, type)
if lookup.found? && lookup.payload.is_a? Array(FilterBlock)
blocks = lookup.payload.as(Array(FilterBlock))
@ -55,17 +55,17 @@ module Kemal
end
# This checks is filter is already defined for the verb/path/type combination
private def filter_for_path_type_defined?(verb, path, type)
private def filter_for_path_type_defined?(verb : String, path : String, type)
lookup = @tree.find radix_path(verb, path, type)
lookup.found? && lookup.payload.is_a? FilterBlock
end
# This returns a lookup for verb/path/type
private def lookup_filters_for_path_type(verb, path, type)
private def lookup_filters_for_path_type(verb : String?, path : String, type)
@tree.find radix_path(verb, path, type)
end
private def radix_path(verb, path, type : Symbol)
private def radix_path(verb : String?, path : String, type : Symbol)
"#{type}/#{verb}/#{path}"
end
@ -77,7 +77,7 @@ module Kemal
@block = ->(context : HTTP::Server::Context) { block.call(context).to_s }
end
def call(context)
def call(context : HTTP::Server::Context)
@block.call(context)
end
end

View file

@ -21,7 +21,7 @@ module Kemal
end
end
def call(env)
def call(env : HTTP::Server::Context)
call_next(env)
end
@ -40,7 +40,7 @@ module Kemal
# puts "If the path is / i will be doing some processing here."
# end
# end
def only_match?(env)
def only_match?(env : HTTP::Server::Context)
@@only_routes_tree.find(radix_path(env.request.method, env.request.path)).found?
end
@ -59,7 +59,7 @@ module Kemal
# puts "If the path is not / i will be doing some processing here."
# end
# end
def exclude_match?(env)
def exclude_match?(env : HTTP::Server::Context)
@@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found?
end

View file

@ -93,7 +93,7 @@ end
# Optionally you can override the mime_type
#
# send_file env, "./path/to/file", "image/jpeg"
def send_file(env, path : String, mime_type : String? = nil)
def send_file(env : HTTP::Server::Context, path : String, mime_type : String? = nil)
config = Kemal.config.serve_static
file_path = File.expand_path(path, Dir.current)
mime_type ||= Kemal::Utils.mime_type(file_path)
@ -128,7 +128,7 @@ def send_file(env, path : String, mime_type : String? = nil)
return
end
private def multipart(file, env)
private def multipart(file, env : HTTP::Server::Context)
# See http://httpwg.org/specs/rfc7233.html
fileb = file.size
@ -187,7 +187,7 @@ end
# Optionally you can override the mime_type
#
# send_file env, data_slice, "image/jpeg"
def send_file(env, data : Slice(UInt8), mime_type : String? = nil)
def send_file(env : HTTP::Server::Context, data : Slice(UInt8), mime_type : String? = nil)
mime_type ||= "application/octet-stream"
env.response.content_type = mime_type
env.response.content_length = data.bytesize

View file

@ -5,7 +5,7 @@ module Kemal
include HTTP::Handler
INSTANCE = new
def call(context)
def call(context : HTTP::Server::Context)
context.response.headers.add "X-Powered-By", "Kemal"
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
call_next context

View file

@ -15,27 +15,27 @@ module Kemal
@ws_routes = Radix::Tree(String).new
end
def call(context)
def call(context : HTTP::Server::Context)
process_request(context)
end
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
# a corresponding `HEAD` route.
def add_http_route(method, path, &handler : HTTP::Server::Context -> _)
def add_http_route(method : String, path : String, &handler : HTTP::Server::Context -> _)
add_to_http_radix_tree method, path, Route.new(method, path, &handler)
add_to_http_radix_tree("HEAD", path, Route.new("HEAD", path) { |ctx| "" }) if method == "GET"
end
def add_ws_route(path)
def add_ws_route(path : String)
add_to_ws_radix_tree path
end
# Check if a route is defined and returns the lookup
def lookup_route(verb, path)
def lookup_route(verb : String, path : String)
@http_routes.find radix_path(verb, path)
end
def lookup_ws_route(path)
def lookup_ws_route(path : String)
@ws_routes.find "/ws#{path}"
end

View file

@ -4,7 +4,7 @@
module Kemal
class StaticFileHandler < HTTP::StaticFileHandler
def call(context)
def call(context : HTTP::Server::Context)
return call_next(context) if context.request.path.not_nil! == "/"
unless context.request.method == "GET" || context.request.method == "HEAD"
@ -57,7 +57,7 @@ module Kemal
end
end
private def etag(context, file_path)
private def etag(context : HTTP::Server::Context, file_path : String)
etag = %{W/"#{File.lstat(file_path).mtime.epoch.to_s}"}
context.response.headers["ETag"] = etag
return false if !context.request.headers["If-None-Match"]? || context.request.headers["If-None-Match"] != etag

View file

@ -7,7 +7,7 @@ module Kemal
Kemal::RouteHandler::INSTANCE.add_ws_route @path
end
def call(context)
def call(context : HTTP::Server::Context)
return call_next(context) unless context.ws_route_defined?
super
end