Refactour handler radix names
This commit is contained in:
parent
63e613a439
commit
84839f4da7
3 changed files with 16 additions and 16 deletions
|
@ -81,6 +81,6 @@ end
|
||||||
|
|
||||||
Spec.after_each do
|
Spec.after_each do
|
||||||
Kemal.config.clear
|
Kemal.config.clear
|
||||||
Kemal::RouteHandler::INSTANCE.http_routes = Radix::Tree(Route).new
|
Kemal::RouteHandler::INSTANCE.routes = Radix::Tree(Route).new
|
||||||
Kemal::WebSocketHandler::INSTANCE.ws_routes = Radix::Tree(WebSocket).new
|
Kemal::WebSocketHandler::INSTANCE.routes = Radix::Tree(WebSocket).new
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,10 +6,10 @@ module Kemal
|
||||||
class RouteHandler
|
class RouteHandler
|
||||||
include HTTP::Handler
|
include HTTP::Handler
|
||||||
INSTANCE = new
|
INSTANCE = new
|
||||||
property http_routes
|
property routes
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@http_routes = Radix::Tree(Route).new
|
@routes = Radix::Tree(Route).new
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(context : HTTP::Server::Context)
|
def call(context : HTTP::Server::Context)
|
||||||
|
@ -19,13 +19,13 @@ module Kemal
|
||||||
# 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_route(method : String, path : String, &handler : HTTP::Server::Context -> _)
|
def add_route(method : String, path : String, &handler : HTTP::Server::Context -> _)
|
||||||
add_to_http_radix_tree method, path, Route.new(method, path, &handler)
|
add_to_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_radix_tree("HEAD", path, Route.new("HEAD", path) { |ctx| "" }) if method == "GET"
|
||||||
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 : String, path : String)
|
def lookup_route(verb : String, path : String)
|
||||||
@http_routes.find radix_path(verb, path)
|
@routes.find radix_path(verb, path)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Processes the route if it's a match. Otherwise renders 404.
|
# Processes the route if it's a match. Otherwise renders 404.
|
||||||
|
@ -36,7 +36,7 @@ module Kemal
|
||||||
if Kemal.config.error_handlers.size != 0 && Kemal.config.error_handlers.has_key?(context.response.status_code)
|
if Kemal.config.error_handlers.size != 0 && Kemal.config.error_handlers.has_key?(context.response.status_code)
|
||||||
raise Kemal::Exceptions::CustomException.new(context)
|
raise Kemal::Exceptions::CustomException.new(context)
|
||||||
end
|
end
|
||||||
|
|
||||||
context.response.print(content)
|
context.response.print(content)
|
||||||
context
|
context
|
||||||
end
|
end
|
||||||
|
@ -45,9 +45,9 @@ module Kemal
|
||||||
"/#{method.downcase}#{path}"
|
"/#{method.downcase}#{path}"
|
||||||
end
|
end
|
||||||
|
|
||||||
private def add_to_http_radix_tree(method, path, route)
|
private def add_to_radix_tree(method, path, route)
|
||||||
node = radix_path method, path
|
node = radix_path method, path
|
||||||
@http_routes.add node, route
|
@routes.add node, route
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,10 +4,10 @@ module Kemal
|
||||||
class WebSocketHandler
|
class WebSocketHandler
|
||||||
include HTTP::Handler
|
include HTTP::Handler
|
||||||
INSTANCE = new
|
INSTANCE = new
|
||||||
property ws_routes
|
property routes
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@ws_routes = Radix::Tree(WebSocket).new
|
@routes = Radix::Tree(WebSocket).new
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(context : HTTP::Server::Context)
|
def call(context : HTTP::Server::Context)
|
||||||
|
@ -19,16 +19,16 @@ module Kemal
|
||||||
end
|
end
|
||||||
|
|
||||||
def lookup_ws_route(path : String)
|
def lookup_ws_route(path : String)
|
||||||
@ws_routes.find "/ws#{path}"
|
@routes.find "/ws#{path}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_route(path : String, &handler : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
def add_route(path : String, &handler : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
||||||
add_to_ws_radix_tree path, WebSocket.new(path, &handler)
|
add_to_radix_tree path, WebSocket.new(path, &handler)
|
||||||
end
|
end
|
||||||
|
|
||||||
private def add_to_ws_radix_tree(path, websocket)
|
private def add_to_radix_tree(path, websocket)
|
||||||
node = radix_path "ws", path
|
node = radix_path "ws", path
|
||||||
@ws_routes.add node, websocket
|
@routes.add node, websocket
|
||||||
end
|
end
|
||||||
|
|
||||||
private def radix_path(method, path)
|
private def radix_path(method, path)
|
||||||
|
|
Loading…
Reference in a new issue