kemal/src/kemal/route_handler.cr

69 lines
2.0 KiB
Crystal
Raw Normal View History

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
INSTANCE = new
CACHED_ROUTES_LIMIT = 1024
2018-09-16 10:17:51 +00:00
property routes, cached_routes
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"
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)
if route.found?
@cached_routes.clear if @cached_routes.size == CACHED_ROUTES_LIMIT
@cached_routes[lookup_path] = route
end
2018-09-16 10:17:51 +00:00
route
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?
return if context.response.closed?
content = context.route.handler.call(context)
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
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
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
end
end
2014-06-11 23:41:02 +00:00
end