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
|
|
|
|
# Kemal::RouteHandler is the main handler which handles all the HTTP requests. Routing, parsing, rendering e.g
|
|
|
|
# are done in this handler.
|
2016-12-24 11:22:44 +00:00
|
|
|
class RouteHandler
|
|
|
|
include HTTP::Handler
|
2016-07-17 11:43:13 +00:00
|
|
|
INSTANCE = new
|
2017-10-06 09:46:58 +00:00
|
|
|
property 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
|
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)
|
|
|
|
add_to_radix_tree("HEAD", path, Route.new("HEAD", path) { |ctx| "" }) if method == "GET"
|
2017-08-20 17:01:49 +00:00
|
|
|
end
|
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
# Check if a route is defined and returns the lookup
|
2017-08-25 13:41:02 +00:00
|
|
|
def lookup_route(verb : String, path : String)
|
2017-10-06 09:46:58 +00:00
|
|
|
@routes.find radix_path(verb, path)
|
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)
|
2016-07-17 11:43:13 +00:00
|
|
|
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined?
|
2017-09-04 20:46:53 +00:00
|
|
|
content = context.route.handler.call(context)
|
2017-10-05 16:52:57 +00:00
|
|
|
|
|
|
|
if Kemal.config.error_handlers.size != 0 && 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)
|
2016-07-17 11:43:13 +00:00
|
|
|
"/#{method.downcase}#{path}"
|
|
|
|
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
|