2016-01-24 18:50:54 +00:00
|
|
|
require "radix"
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
module Kemal
|
2016-12-24 11:22:44 +00:00
|
|
|
class RouteHandler
|
|
|
|
include HTTP::Handler
|
2018-03-17 14:58:19 +00:00
|
|
|
|
2018-09-16 12:34:43 +00:00
|
|
|
INSTANCE = new
|
|
|
|
CACHED_ROUTES_LIMIT = 1024
|
2018-09-16 10:17:51 +00:00
|
|
|
property routes, cached_routes
|
2016-02-16 20:07:58 +00:00
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
def initialize
|
2017-10-06 09:46:58 +00:00
|
|
|
@routes = Radix::Tree(Route).new
|
2018-09-16 10:17:51 +00:00
|
|
|
@cached_routes = Hash(String, Radix::Result(Route)).new
|
2016-07-17 11:43:13 +00:00
|
|
|
end
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2017-08-25 13:41:02 +00:00
|
|
|
def call(context : HTTP::Server::Context)
|
2016-07-17 11:43:13 +00:00
|
|
|
process_request(context)
|
|
|
|
end
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
|
|
|
|
# a corresponding `HEAD` route.
|
2017-10-02 20:56:02 +00:00
|
|
|
def add_route(method : String, path : String, &handler : HTTP::Server::Context -> _)
|
2017-10-06 09:46:58 +00:00
|
|
|
add_to_radix_tree method, path, Route.new(method, path, &handler)
|
2020-02-21 00:17:50 +00:00
|
|
|
# add_to_radix_tree("HEAD", path, Route.new("HEAD", path) { }) if method == "GET"
|
2017-08-20 17:01:49 +00:00
|
|
|
end
|
|
|
|
|
2018-09-16 10:17:51 +00:00
|
|
|
# Looks up the route from the Radix::Tree for the first time and caches to improve performance.
|
2017-08-25 13:41:02 +00:00
|
|
|
def lookup_route(verb : String, path : String)
|
2018-09-16 10:17:51 +00:00
|
|
|
lookup_path = radix_path(verb, path)
|
|
|
|
|
|
|
|
if cached_route = @cached_routes[lookup_path]?
|
|
|
|
return cached_route
|
|
|
|
end
|
|
|
|
|
|
|
|
route = @routes.find(lookup_path)
|
|
|
|
|
2018-09-17 06:51:56 +00:00
|
|
|
if route.found?
|
|
|
|
@cached_routes.clear if @cached_routes.size == CACHED_ROUTES_LIMIT
|
2018-09-16 12:34:43 +00:00
|
|
|
@cached_routes[lookup_path] = route
|
|
|
|
end
|
2018-09-16 10:17:51 +00:00
|
|
|
|
|
|
|
route
|
2017-08-20 17:01:49 +00:00
|
|
|
end
|
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
# Processes the route if it's a match. Otherwise renders 404.
|
2017-02-13 20:39:40 +00:00
|
|
|
private def process_request(context)
|
2018-08-13 16:21:18 +00:00
|
|
|
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_found?
|
2019-08-05 15:34:26 +00:00
|
|
|
return if context.response.closed?
|
2017-09-04 20:46:53 +00:00
|
|
|
content = context.route.handler.call(context)
|
2017-10-05 16:52:57 +00:00
|
|
|
|
2017-10-06 10:41:22 +00:00
|
|
|
if !Kemal.config.error_handlers.empty? && Kemal.config.error_handlers.has_key?(context.response.status_code)
|
2016-07-17 11:43:13 +00:00
|
|
|
raise Kemal::Exceptions::CustomException.new(context)
|
|
|
|
end
|
2017-10-06 09:46:58 +00:00
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
context.response.print(content)
|
|
|
|
context
|
2016-05-05 19:35:36 +00:00
|
|
|
end
|
2016-01-22 20:14:22 +00:00
|
|
|
|
2017-08-20 17:01:49 +00:00
|
|
|
private def radix_path(method, path)
|
2018-11-01 11:29:05 +00:00
|
|
|
'/' + method.downcase + path
|
2016-07-17 11:43:13 +00:00
|
|
|
end
|
2016-01-22 20:14:22 +00:00
|
|
|
|
2017-10-06 09:46:58 +00:00
|
|
|
private def add_to_radix_tree(method, path, route)
|
2016-07-17 11:43:13 +00:00
|
|
|
node = radix_path method, path
|
2017-10-06 09:46:58 +00:00
|
|
|
@routes.add node, route
|
2017-08-20 17:01:49 +00:00
|
|
|
end
|
2016-01-22 20:14:22 +00:00
|
|
|
end
|
2014-06-11 23:41:02 +00:00
|
|
|
end
|