Modularize handlers
This commit is contained in:
parent
ce9e924d00
commit
62946fd987
3 changed files with 23 additions and 4 deletions
|
@ -120,4 +120,17 @@ describe "Handler" do
|
||||||
client_response = call_request_on_app(request)
|
client_response = call_request_on_app(request)
|
||||||
client_response.body.should eq "OnlyExcludePost"
|
client_response.body.should eq "OnlyExcludePost"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "adds a handler at given position" do
|
||||||
|
post_handler = PostOnlyHandler.new
|
||||||
|
add_handler post_handler, 1
|
||||||
|
Kemal.config.handlers[1].should eq post_handler
|
||||||
|
end
|
||||||
|
|
||||||
|
it "assigns custom handlers" do
|
||||||
|
post_only_handler = PostOnlyHandler.new
|
||||||
|
post_exclude_handler = PostExcludeHandler.new
|
||||||
|
Kemal.config.handlers = [post_only_handler, post_exclude_handler]
|
||||||
|
Kemal.config.handlers.should eq [post_only_handler, post_exclude_handler]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,6 +17,7 @@ module Kemal
|
||||||
property host_binding, ssl, port, env, public_folder, logging, running,
|
property host_binding, ssl, port, env, public_folder, logging, running,
|
||||||
always_rescue, serve_static : (Bool | Hash(String, Bool)), server, extra_options,
|
always_rescue, serve_static : (Bool | Hash(String, Bool)), server, extra_options,
|
||||||
shutdown_message
|
shutdown_message
|
||||||
|
getter custom_handler_position
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@host_binding = "0.0.0.0"
|
@host_binding = "0.0.0.0"
|
||||||
|
@ -59,9 +60,14 @@ module Kemal
|
||||||
HANDLERS
|
HANDLERS
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_handler(handler : HTTP::Handler | HTTP::WebSocketHandler)
|
def handlers=(handlers : Array(HTTP::Handler))
|
||||||
|
clear
|
||||||
|
handlers.each { |handler| HANDLERS << handler }
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_handler(handler : HTTP::Handler | HTTP::WebSocketHandler, position = Kemal.config.custom_handler_position)
|
||||||
setup
|
setup
|
||||||
HANDLERS.insert @custom_handler_position, handler
|
HANDLERS.insert position, handler
|
||||||
@custom_handler_position = @custom_handler_position + 1
|
@custom_handler_position = @custom_handler_position + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,8 @@
|
||||||
# - Kemal::StaticFileHandler
|
# - Kemal::StaticFileHandler
|
||||||
# - Here goes custom handlers
|
# - Here goes custom handlers
|
||||||
# - Kemal::RouteHandler
|
# - Kemal::RouteHandler
|
||||||
def add_handler(handler)
|
def add_handler(handler, position = Kemal.config.custom_handler_position)
|
||||||
Kemal.config.add_handler handler
|
Kemal.config.add_handler handler, position
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sets public folder from which the static assets will be served.
|
# Sets public folder from which the static assets will be served.
|
||||||
|
|
Loading…
Reference in a new issue