Add more types to method signatures
This commit is contained in:
parent
00217d9545
commit
79e324efaf
11 changed files with 30 additions and 30 deletions
|
@ -3,7 +3,7 @@ module Kemal
|
||||||
abstract class BaseLogHandler
|
abstract class BaseLogHandler
|
||||||
include HTTP::Handler
|
include HTTP::Handler
|
||||||
|
|
||||||
abstract def call(context)
|
abstract def call(context : HTTP::Server::Context)
|
||||||
abstract def write(message : String)
|
abstract def write(message : String)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,7 @@ module Kemal
|
||||||
include HTTP::Handler
|
include HTTP::Handler
|
||||||
INSTANCE = new
|
INSTANCE = new
|
||||||
|
|
||||||
def call(context)
|
def call(context : HTTP::Server::Context)
|
||||||
begin
|
begin
|
||||||
call_next(context)
|
call_next(context)
|
||||||
rescue ex : Kemal::Exceptions::RouteNotFound
|
rescue ex : Kemal::Exceptions::RouteNotFound
|
||||||
|
|
|
@ -7,7 +7,7 @@ module Kemal
|
||||||
@handler = io
|
@handler = io
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(context)
|
def call(context : HTTP::Server::Context)
|
||||||
time = Time.now
|
time = Time.now
|
||||||
call_next(context)
|
call_next(context)
|
||||||
elapsed_text = elapsed_text(Time.now - time)
|
elapsed_text = elapsed_text(Time.now - time)
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
# Exceptions for 404 and custom errors are defined here.
|
# Exceptions for 404 and custom errors are defined here.
|
||||||
module Kemal::Exceptions
|
module Kemal::Exceptions
|
||||||
class InvalidPathStartException < Exception
|
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}\""
|
super "Route declaration #{method} \"#{path}\" needs to start with '/', should be #{method} \"/#{path}\""
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class RouteNotFound < Exception
|
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."
|
super "Requested path: '#{context.request.override_method.as(String)}:#{context.request.path}' was not found."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class CustomException < Exception
|
class CustomException < Exception
|
||||||
def initialize(context)
|
def initialize(context : HTTP::Server::Context)
|
||||||
super "Rendered error with #{context.response.status_code}"
|
super "Rendered error with #{context.response.status_code}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,7 +11,7 @@ module Kemal
|
||||||
end
|
end
|
||||||
|
|
||||||
# The call order of the filters is before_all -> before_x -> X -> after_x -> after_all
|
# 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?
|
return call_next(context) unless context.route_defined?
|
||||||
call_block_for_path_type("ALL", context.request.path, :before, context)
|
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)
|
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.
|
# :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.
|
# 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)
|
lookup = lookup_filters_for_path_type(verb, path, type)
|
||||||
if lookup.found? && lookup.payload.is_a?(Array(FilterBlock))
|
if lookup.found? && lookup.payload.is_a?(Array(FilterBlock))
|
||||||
(lookup.payload.as(Array(FilterBlock))) << FilterBlock.new(&block)
|
(lookup.payload.as(Array(FilterBlock))) << FilterBlock.new(&block)
|
||||||
|
@ -36,17 +36,17 @@ module Kemal
|
||||||
end
|
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`
|
# 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
|
_add_route_filter verb, path, :before, &block
|
||||||
end
|
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`
|
# 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
|
_add_route_filter verb, path, :after, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
# This will fetch the block for the verb/path/type from the tree and call it.
|
# 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)
|
lookup = lookup_filters_for_path_type(verb, path, type)
|
||||||
if lookup.found? && lookup.payload.is_a? Array(FilterBlock)
|
if lookup.found? && lookup.payload.is_a? Array(FilterBlock)
|
||||||
blocks = lookup.payload.as(Array(FilterBlock))
|
blocks = lookup.payload.as(Array(FilterBlock))
|
||||||
|
@ -55,17 +55,17 @@ module Kemal
|
||||||
end
|
end
|
||||||
|
|
||||||
# This checks is filter is already defined for the verb/path/type combination
|
# 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 = @tree.find radix_path(verb, path, type)
|
||||||
lookup.found? && lookup.payload.is_a? FilterBlock
|
lookup.found? && lookup.payload.is_a? FilterBlock
|
||||||
end
|
end
|
||||||
|
|
||||||
# This returns a lookup for verb/path/type
|
# 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)
|
@tree.find radix_path(verb, path, type)
|
||||||
end
|
end
|
||||||
|
|
||||||
private def radix_path(verb, path, type : Symbol)
|
private def radix_path(verb : String?, path : String, type : Symbol)
|
||||||
"#{type}/#{verb}/#{path}"
|
"#{type}/#{verb}/#{path}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ module Kemal
|
||||||
@block = ->(context : HTTP::Server::Context) { block.call(context).to_s }
|
@block = ->(context : HTTP::Server::Context) { block.call(context).to_s }
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(context)
|
def call(context : HTTP::Server::Context)
|
||||||
@block.call(context)
|
@block.call(context)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,7 @@ module Kemal
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env : HTTP::Server::Context)
|
||||||
call_next(env)
|
call_next(env)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ module Kemal
|
||||||
# puts "If the path is / i will be doing some processing here."
|
# puts "If the path is / i will be doing some processing here."
|
||||||
# end
|
# end
|
||||||
# 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?
|
@@only_routes_tree.find(radix_path(env.request.method, env.request.path)).found?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ module Kemal
|
||||||
# puts "If the path is not / i will be doing some processing here."
|
# puts "If the path is not / i will be doing some processing here."
|
||||||
# end
|
# end
|
||||||
# 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?
|
@@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -93,7 +93,7 @@ end
|
||||||
# Optionally you can override the mime_type
|
# Optionally you can override the mime_type
|
||||||
#
|
#
|
||||||
# send_file env, "./path/to/file", "image/jpeg"
|
# 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
|
config = Kemal.config.serve_static
|
||||||
file_path = File.expand_path(path, Dir.current)
|
file_path = File.expand_path(path, Dir.current)
|
||||||
mime_type ||= Kemal::Utils.mime_type(file_path)
|
mime_type ||= Kemal::Utils.mime_type(file_path)
|
||||||
|
@ -128,7 +128,7 @@ def send_file(env, path : String, mime_type : String? = nil)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
private def multipart(file, env)
|
private def multipart(file, env : HTTP::Server::Context)
|
||||||
# See http://httpwg.org/specs/rfc7233.html
|
# See http://httpwg.org/specs/rfc7233.html
|
||||||
fileb = file.size
|
fileb = file.size
|
||||||
|
|
||||||
|
@ -187,7 +187,7 @@ end
|
||||||
# Optionally you can override the mime_type
|
# Optionally you can override the mime_type
|
||||||
#
|
#
|
||||||
# send_file env, data_slice, "image/jpeg"
|
# 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"
|
mime_type ||= "application/octet-stream"
|
||||||
env.response.content_type = mime_type
|
env.response.content_type = mime_type
|
||||||
env.response.content_length = data.bytesize
|
env.response.content_length = data.bytesize
|
||||||
|
|
|
@ -5,7 +5,7 @@ module Kemal
|
||||||
include HTTP::Handler
|
include HTTP::Handler
|
||||||
INSTANCE = new
|
INSTANCE = new
|
||||||
|
|
||||||
def call(context)
|
def call(context : HTTP::Server::Context)
|
||||||
context.response.headers.add "X-Powered-By", "Kemal"
|
context.response.headers.add "X-Powered-By", "Kemal"
|
||||||
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
|
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
|
||||||
call_next context
|
call_next context
|
||||||
|
|
|
@ -15,27 +15,27 @@ module Kemal
|
||||||
@ws_routes = Radix::Tree(String).new
|
@ws_routes = Radix::Tree(String).new
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(context)
|
def call(context : HTTP::Server::Context)
|
||||||
process_request(context)
|
process_request(context)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
|
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
|
||||||
# a corresponding `HEAD` route.
|
# 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 method, path, Route.new(method, path, &handler)
|
||||||
add_to_http_radix_tree("HEAD", path, Route.new("HEAD", path) { |ctx| "" }) if method == "GET"
|
add_to_http_radix_tree("HEAD", path, Route.new("HEAD", path) { |ctx| "" }) if method == "GET"
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_ws_route(path)
|
def add_ws_route(path : String)
|
||||||
add_to_ws_radix_tree path
|
add_to_ws_radix_tree path
|
||||||
end
|
end
|
||||||
|
|
||||||
# Check if a route is defined and returns the lookup
|
# 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)
|
@http_routes.find radix_path(verb, path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def lookup_ws_route(path)
|
def lookup_ws_route(path : String)
|
||||||
@ws_routes.find "/ws#{path}"
|
@ws_routes.find "/ws#{path}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
module Kemal
|
module Kemal
|
||||||
class StaticFileHandler < HTTP::StaticFileHandler
|
class StaticFileHandler < HTTP::StaticFileHandler
|
||||||
def call(context)
|
def call(context : HTTP::Server::Context)
|
||||||
return call_next(context) if context.request.path.not_nil! == "/"
|
return call_next(context) if context.request.path.not_nil! == "/"
|
||||||
|
|
||||||
unless context.request.method == "GET" || context.request.method == "HEAD"
|
unless context.request.method == "GET" || context.request.method == "HEAD"
|
||||||
|
@ -57,7 +57,7 @@ module Kemal
|
||||||
end
|
end
|
||||||
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}"}
|
etag = %{W/"#{File.lstat(file_path).mtime.epoch.to_s}"}
|
||||||
context.response.headers["ETag"] = etag
|
context.response.headers["ETag"] = etag
|
||||||
return false if !context.request.headers["If-None-Match"]? || context.request.headers["If-None-Match"] != etag
|
return false if !context.request.headers["If-None-Match"]? || context.request.headers["If-None-Match"] != etag
|
||||||
|
|
|
@ -7,7 +7,7 @@ module Kemal
|
||||||
Kemal::RouteHandler::INSTANCE.add_ws_route @path
|
Kemal::RouteHandler::INSTANCE.add_ws_route @path
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(context)
|
def call(context : HTTP::Server::Context)
|
||||||
return call_next(context) unless context.ws_route_defined?
|
return call_next(context) unless context.ws_route_defined?
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue