2017-03-03 20:56:29 +00:00
|
|
|
module Kemal
|
|
|
|
# `Kemal::Handler` is a subclass of `HTTP::Handler`.
|
2017-10-06 11:53:53 +00:00
|
|
|
#
|
2017-03-03 20:56:29 +00:00
|
|
|
# It adds `only`, `only_match?`, `exclude`, `exclude_match?`.
|
2018-03-17 12:35:33 +00:00
|
|
|
# These methods are useful for the conditional execution of custom handlers .
|
2017-03-03 20:56:29 +00:00
|
|
|
class Handler
|
|
|
|
include HTTP::Handler
|
2018-03-17 14:58:19 +00:00
|
|
|
|
2017-03-03 20:56:29 +00:00
|
|
|
@@only_routes_tree = Radix::Tree(String).new
|
|
|
|
@@exclude_routes_tree = Radix::Tree(String).new
|
2016-11-09 15:18:39 +00:00
|
|
|
|
2017-03-03 20:56:29 +00:00
|
|
|
macro only(paths, method = "GET")
|
2017-10-06 11:55:37 +00:00
|
|
|
class_name = {{@type.name}}
|
2018-11-14 10:47:46 +00:00
|
|
|
method_downcase = {{method.downcase}}
|
2018-11-01 11:29:05 +00:00
|
|
|
class_name_method = "#{class_name}/#{method_downcase}"
|
2017-10-06 11:55:37 +00:00
|
|
|
({{paths}}).each do |path|
|
2018-11-01 11:29:05 +00:00
|
|
|
@@only_routes_tree.add class_name_method + path, '/' + method_downcase + path
|
2017-10-06 11:55:37 +00:00
|
|
|
end
|
2016-11-09 15:18:39 +00:00
|
|
|
end
|
|
|
|
|
2017-03-03 20:56:29 +00:00
|
|
|
macro exclude(paths, method = "GET")
|
2017-10-06 11:55:37 +00:00
|
|
|
class_name = {{@type.name}}
|
2018-11-14 10:47:46 +00:00
|
|
|
method_downcase = {{method.downcase}}
|
2018-11-10 21:58:41 +00:00
|
|
|
class_name_method = "#{class_name}/#{method_downcase}"
|
2017-10-06 11:55:37 +00:00
|
|
|
({{paths}}).each do |path|
|
2018-11-01 11:29:05 +00:00
|
|
|
@@exclude_routes_tree.add class_name_method + path, '/' + method_downcase + path
|
2017-10-06 11:55:37 +00:00
|
|
|
end
|
2016-11-09 15:18:39 +00:00
|
|
|
end
|
|
|
|
|
2022-06-29 10:31:28 +00:00
|
|
|
def call(context : HTTP::Server::Context)
|
|
|
|
call_next(context)
|
2017-03-03 20:56:29 +00:00
|
|
|
end
|
2016-11-09 15:18:39 +00:00
|
|
|
|
2017-03-03 20:56:29 +00:00
|
|
|
# Processes the path based on `only` paths which is a `Array(String)`.
|
|
|
|
# If the path is not found on `only` conditions the handler will continue processing.
|
|
|
|
# If the path is found in `only` conditions it'll stop processing and will pass the request
|
|
|
|
# to next handler.
|
|
|
|
#
|
|
|
|
# However this is not done automatically. All handlers must inherit from `Kemal::Handler`.
|
|
|
|
#
|
2017-10-06 11:53:53 +00:00
|
|
|
# ```
|
|
|
|
# class OnlyHandler < Kemal::Handler
|
|
|
|
# only ["/"]
|
2017-03-03 20:56:29 +00:00
|
|
|
#
|
2017-10-06 11:53:53 +00:00
|
|
|
# def call(env)
|
|
|
|
# return call_next(env) unless only_match?(env)
|
|
|
|
# puts "If the path is / i will be doing some processing here."
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# ```
|
2017-08-25 13:41:02 +00:00
|
|
|
def only_match?(env : HTTP::Server::Context)
|
2017-03-03 20:56:29 +00:00
|
|
|
@@only_routes_tree.find(radix_path(env.request.method, env.request.path)).found?
|
|
|
|
end
|
2016-11-09 15:18:39 +00:00
|
|
|
|
2017-03-03 20:56:29 +00:00
|
|
|
# Processes the path based on `exclude` paths which is a `Array(String)`.
|
|
|
|
# If the path is not found on `exclude` conditions the handler will continue processing.
|
|
|
|
# If the path is found in `exclude` conditions it'll stop processing and will pass the request
|
|
|
|
# to next handler.
|
|
|
|
#
|
|
|
|
# However this is not done automatically. All handlers must inherit from `Kemal::Handler`.
|
|
|
|
#
|
2017-10-06 11:53:53 +00:00
|
|
|
# ```
|
|
|
|
# class ExcludeHandler < Kemal::Handler
|
|
|
|
# exclude ["/"]
|
2017-03-03 20:56:29 +00:00
|
|
|
#
|
2017-10-06 11:53:53 +00:00
|
|
|
# def call(env)
|
|
|
|
# return call_next(env) if exclude_match?(env)
|
|
|
|
# puts "If the path is not / i will be doing some processing here."
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# ```
|
2017-08-25 13:41:02 +00:00
|
|
|
def exclude_match?(env : HTTP::Server::Context)
|
2017-03-03 20:56:29 +00:00
|
|
|
@@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found?
|
|
|
|
end
|
2016-11-09 15:18:39 +00:00
|
|
|
|
2017-08-24 20:32:43 +00:00
|
|
|
private def radix_path(method : String, path : String)
|
2017-03-03 20:56:29 +00:00
|
|
|
"#{self.class}/#{method.downcase}#{path}"
|
|
|
|
end
|
2016-11-09 15:18:39 +00:00
|
|
|
end
|
|
|
|
end
|