Add app reference to the handlers which are bound to the app

This commit is contained in:
Johannes Müller 2017-10-18 19:48:01 +02:00 committed by sdogruyol
parent dc93bb2235
commit 72bcac6dd5
12 changed files with 58 additions and 49 deletions

View file

@ -64,7 +64,6 @@ describe "Context" do
io = IO::Memory.new io = IO::Memory.new
response = HTTP::Server::Response.new(io) response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response) context = HTTP::Server::Context.new(request, response)
context.app = app
app.filter_handler.call(context) app.filter_handler.call(context)
app.route_handler.call(context) app.route_handler.call(context)
context.store["key"].should eq "value" context.store["key"].should eq "value"

View file

@ -12,11 +12,11 @@ class CustomLogHandler < Kemal::BaseLogHandler
end end
end end
def create_request_and_return_io(handler, request) def create_request_and_return_io(handler, request, app = Kemal.application)
io = IO::Memory.new io = IO::Memory.new
response = HTTP::Server::Response.new(io) response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response) context = HTTP::Server::Context.new(request, response)
context.app = Kemal.application context.app = app
handler.call(context) handler.call(context)
response.close response.close
io.rewind io.rewind

View file

@ -26,9 +26,8 @@ describe "Kemal::ExceptionHandler" do
app.get "/" do |env| app.get "/" do |env|
env.response.status_code = 403 env.response.status_code = 403
end end
context.app = app
subject = Kemal::ExceptionHandler.new(app) subject = Kemal::ExceptionHandler.new(app)
subject.next = Kemal::RouteHandler.new subject.next = app.route_handler
subject.call(context) subject.call(context)
response.close response.close
io.rewind io.rewind
@ -50,9 +49,8 @@ describe "Kemal::ExceptionHandler" do
app.get "/" do |env| app.get "/" do |env|
env.response.status_code = 500 env.response.status_code = 500
end end
context.app = app
subject = Kemal::ExceptionHandler.new(app) subject = Kemal::ExceptionHandler.new(app)
subject.next = Kemal::RouteHandler.new subject.next = app.route_handler
subject.call(context) subject.call(context)
response.close response.close
io.rewind io.rewind
@ -75,9 +73,8 @@ describe "Kemal::ExceptionHandler" do
env.response.content_type = "application/json" env.response.content_type = "application/json"
env.response.status_code = 500 env.response.status_code = 500
end end
context.app = app
subject = Kemal::ExceptionHandler.new(app) subject = Kemal::ExceptionHandler.new(app)
subject.next = Kemal::RouteHandler.new subject.next = app.route_handler
subject.call(context) subject.call(context)
response.close response.close
io.rewind io.rewind
@ -100,9 +97,8 @@ describe "Kemal::ExceptionHandler" do
env.response.content_type = "application/json" env.response.content_type = "application/json"
env.response.status_code = 500 env.response.status_code = 500
end end
context.app = app subject = Kemal::ExceptionHandler.new(app)
subject = Kemal::ExceptionHandler.new(Kemal::Base.new) subject.next = app.route_handler
subject.next = Kemal::RouteHandler.new
subject.call(context) subject.call(context)
response.close response.close
io.rewind io.rewind

View file

@ -69,8 +69,8 @@ end
describe "Handler" do describe "Handler" do
it "adds custom handler before before_*" do it "adds custom handler before before_*" do
filter_middleware = Kemal::FilterHandler.new app = Kemal::Base.new
Kemal.application.add_filter_handler filter_middleware filter_middleware = Kemal::FilterHandler.new(app)
filter_middleware._add_route_filter("GET", "/", :before) do |env| filter_middleware._add_route_filter("GET", "/", :before) do |env|
env.response << " is" env.response << " is"
end end
@ -78,7 +78,6 @@ describe "Handler" do
filter_middleware._add_route_filter("GET", "/", :before) do |env| filter_middleware._add_route_filter("GET", "/", :before) do |env|
env.response << " so" env.response << " so"
end end
app = Kemal::Base.new
app.add_filter_handler filter_middleware app.add_filter_handler filter_middleware
app.add_handler CustomTestHandler.new app.add_handler CustomTestHandler.new

View file

@ -5,7 +5,7 @@ describe "Kemal::FilterHandler" do
test_filter = FilterTest.new test_filter = FilterTest.new
test_filter.modified = "false" test_filter.modified = "false"
filter_middleware = Kemal::FilterHandler.new filter_middleware = Kemal::FilterHandler.new(Kemal.application)
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = "true" } filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = "true" }
kemal = Kemal.application.route_handler kemal = Kemal.application.route_handler
@ -23,7 +23,7 @@ describe "Kemal::FilterHandler" do
test_filter = FilterTest.new test_filter = FilterTest.new
test_filter.modified = "false" test_filter.modified = "false"
filter_middleware = Kemal::FilterHandler.new filter_middleware = Kemal::FilterHandler.new(Kemal.application)
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
kemal = Kemal.application.route_handler kemal = Kemal.application.route_handler
@ -49,7 +49,7 @@ describe "Kemal::FilterHandler" do
test_filter = FilterTest.new test_filter = FilterTest.new
test_filter.modified = "false" test_filter.modified = "false"
filter_middleware = Kemal::FilterHandler.new filter_middleware = Kemal::FilterHandler.new(Kemal.application)
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
filter_middleware._add_route_filter("POST", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("POST", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
@ -77,7 +77,7 @@ describe "Kemal::FilterHandler" do
test_filter = FilterTest.new test_filter = FilterTest.new
test_filter.modified = "false" test_filter.modified = "false"
filter_middleware = Kemal::FilterHandler.new filter_middleware = Kemal::FilterHandler.new(Kemal.application)
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = "true" } filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = "true" }
kemal = Kemal.application.route_handler kemal = Kemal.application.route_handler
@ -95,7 +95,7 @@ describe "Kemal::FilterHandler" do
test_filter = FilterTest.new test_filter = FilterTest.new
test_filter.modified = "false" test_filter.modified = "false"
filter_middleware = Kemal::FilterHandler.new filter_middleware = Kemal::FilterHandler.new(Kemal.application)
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
kemal = Kemal.application.route_handler kemal = Kemal.application.route_handler
@ -121,7 +121,7 @@ describe "Kemal::FilterHandler" do
test_filter = FilterTest.new test_filter = FilterTest.new
test_filter.modified = "false" test_filter.modified = "false"
filter_middleware = Kemal::FilterHandler.new filter_middleware = Kemal::FilterHandler.new(Kemal.application)
filter_middleware._add_route_filter("ALL", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("ALL", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
filter_middleware._add_route_filter("POST", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("POST", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
@ -152,7 +152,7 @@ describe "Kemal::FilterHandler" do
test_filter_third = FilterTest.new test_filter_third = FilterTest.new
test_filter_third.modified = "false" test_filter_third.modified = "false"
filter_middleware = Kemal::FilterHandler.new filter_middleware = Kemal::FilterHandler.new(Kemal.application)
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_second.modified = test_filter_second.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_second.modified = test_filter_second.modified == "true" ? "false" : "true" }
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_third.modified = test_filter_third.modified == "true" ? "false" : "true" } filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_third.modified = test_filter_third.modified == "true" ? "false" : "true" }

View file

@ -1,6 +1,6 @@
require "./spec_helper" require "./spec_helper"
private def create_ws_request_and_return_io(handler, request, app) private def create_ws_request_and_return_io(handler, request, app = Kemal.application)
io = IO::Memory.new io = IO::Memory.new
response = HTTP::Server::Response.new(io) response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response) context = HTTP::Server::Context.new(request, response)
@ -16,8 +16,8 @@ end
describe "Kemal::WebSocketHandler" do describe "Kemal::WebSocketHandler" do
it "doesn't match on wrong route" do it "doesn't match on wrong route" do
app = Kemal::Base.new app = Kemal::Base.new
handler = Kemal::WebSocketHandler.new handler = app.websocket_handler
handler.next = Kemal::RouteHandler.new handler.next = app.route_handler
app.ws "/" { } app.ws "/" { }
headers = HTTP::Headers{ headers = HTTP::Headers{
"Upgrade" => "websocket", "Upgrade" => "websocket",
@ -28,7 +28,6 @@ describe "Kemal::WebSocketHandler" do
io = IO::Memory.new io = IO::Memory.new
response = HTTP::Server::Response.new(io) response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response) context = HTTP::Server::Context.new(request, response)
context.app = app
expect_raises(Kemal::Exceptions::RouteNotFound) do expect_raises(Kemal::Exceptions::RouteNotFound) do
handler.call context handler.call context
@ -37,7 +36,7 @@ describe "Kemal::WebSocketHandler" do
it "matches on given route" do it "matches on given route" do
app = Kemal::Base.new app = Kemal::Base.new
handler = Kemal::WebSocketHandler.new handler = app.websocket_handler
app.ws "/" { |socket, context| socket.send("Match") } app.ws "/" { |socket, context| socket.send("Match") }
app.ws "/no_match" { |socket, context| socket.send "No Match" } app.ws "/no_match" { |socket, context| socket.send "No Match" }
headers = HTTP::Headers{ headers = HTTP::Headers{
@ -54,7 +53,7 @@ describe "Kemal::WebSocketHandler" do
it "fetches named url parameters" do it "fetches named url parameters" do
app = Kemal::Base.new app = Kemal::Base.new
handler = Kemal::WebSocketHandler.new handler = app.websocket_handler
app.ws "/:id" { |s, c| c.params.url["id"] } app.ws "/:id" { |s, c| c.params.url["id"] }
headers = HTTP::Headers{ headers = HTTP::Headers{
"Upgrade" => "websocket", "Upgrade" => "websocket",
@ -69,15 +68,14 @@ describe "Kemal::WebSocketHandler" do
it "matches correct verb" do it "matches correct verb" do
app = Kemal::Base.new app = Kemal::Base.new
handler = Kemal::WebSocketHandler.new handler = app.websocket_handler
handler.next = Kemal::RouteHandler.new handler.next = app.route_handler
app.ws "/" { } app.ws "/" { }
app.get "/" { "get" } app.get "/" { "get" }
request = HTTP::Request.new("GET", "/") request = HTTP::Request.new("GET", "/")
io = IO::Memory.new io = IO::Memory.new
response = HTTP::Server::Response.new(io) response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response) context = HTTP::Server::Context.new(request, response)
context.app = app
handler.call(context) handler.call(context)
response.close response.close
io.rewind io.rewind

View file

@ -15,18 +15,19 @@ class Kemal::Base
include Base::Builder include Base::Builder
# :nodoc: # :nodoc:
getter route_handler = Kemal::RouteHandler.new # TODO: These ivars are initialized in the constructor, but their values depend on `self`.
getter! route_handler : RouteHandler?
# :nodoc: # :nodoc:
getter filter_handler = Kemal::FilterHandler.new getter! filter_handler : FilterHandler?
# :nodoc: # :nodoc:
getter websocket_handler = Kemal::WebSocketHandler.new getter! websocket_handler : WebSocketHandler?
getter handlers = [] of HTTP::Handler getter handlers = [] of HTTP::Handler
getter error_handlers = {} of Int32 => HTTP::Server::Context, Exception -> String getter error_handlers = {} of Int32 => HTTP::Server::Context, Exception -> String
getter config : Config getter config : Config
property! logger : Kemal::BaseLogHandler property! logger : BaseLogHandler
property! server : HTTP::Server property! server : HTTP::Server
property? running = false property? running = false

View file

@ -19,16 +19,16 @@ module Kemal
call_exception_with_status_code(context, ex, context.response.status_code) call_exception_with_status_code(context, ex, context.response.status_code)
rescue ex : Exception rescue ex : Exception
log("Exception: #{ex.inspect_with_backtrace}") log("Exception: #{ex.inspect_with_backtrace}")
return call_exception_with_status_code(context, ex, 500) if context.app.error_handlers.has_key?(500) return call_exception_with_status_code(context, ex, 500) if app.error_handlers.has_key?(500)
verbosity = context.app.config.env == "production" ? false : true verbosity = app.config.env == "production" ? false : true
return app.render_500(context, ex.inspect_with_backtrace, verbosity) return app.render_500(context, ex.inspect_with_backtrace, verbosity)
end end
end end
private def call_exception_with_status_code(context, exception, status_code) private def call_exception_with_status_code(context, exception, status_code)
if context.app.error_handlers.has_key?(status_code) if !app.error_handlers.empty? && app.error_handlers.has_key?(status_code)
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type") context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
context.response.print context.app.error_handlers[status_code].call(context, exception) context.response.print app.error_handlers[status_code].call(context, exception)
context.response.status_code = status_code context.response.status_code = status_code
context context
end end

View file

@ -3,17 +3,19 @@ module Kemal
class FilterHandler class FilterHandler
include HTTP::Handler include HTTP::Handler
getter app : Kemal::Base
# This middleware is lazily instantiated and added to the handlers as soon as a call to `after_X` or `before_X` is made. # This middleware is lazily instantiated and added to the handlers as soon as a call to `after_X` or `before_X` is made.
def initialize def initialize(@app)
@tree = Radix::Tree(Array(FilterBlock)).new @tree = Radix::Tree(Array(FilterBlock)).new
end end
# The call order of the filters is `before_all -> before_x -> X -> after_x -> after_all`. # The call order of the filters is before_all -> before_x -> X -> after_x -> after_all
def call(context : HTTP::Server::Context) def call(context : HTTP::Server::Context)
return call_next(context) unless context.route_found? return call_next(context) unless app.route_handler.route_defined?(context.request)
call_block_for_path_type("ALL", context.request.path, :before, context) call_block_for_path_type("ALL", context.request.path, :before, context)
call_block_for_path_type(context.request.override_method, context.request.path, :before, context) call_block_for_path_type(context.request.override_method, context.request.path, :before, context)
if context.app.error_handlers.has_key?(context.response.status_code) if !app.error_handlers.empty? && app.error_handlers.has_key?(context.response.status_code)
raise Kemal::Exceptions::CustomException.new(context) raise Kemal::Exceptions::CustomException.new(context)
end end
call_next(context) call_next(context)

View file

@ -49,7 +49,7 @@ module Kemal::FileHelpers
end end
def send_file(env, path : String, mime_type : String? = nil) def send_file(env, path : String, mime_type : String? = nil)
send_file(env, path, env.app.config, mime_type) send_file(env, path, config, mime_type)
end end
private def multipart(file, env : HTTP::Server::Context) private def multipart(file, env : HTTP::Server::Context)

View file

@ -8,7 +8,9 @@ module Kemal
CACHED_ROUTES_LIMIT = 1024 CACHED_ROUTES_LIMIT = 1024
property routes, cached_routes property routes, cached_routes
def initialize getter app : Kemal::Base
def initialize(@app)
@routes = Radix::Tree(Route).new @routes = Radix::Tree(Route).new
@cached_routes = Hash(String, Radix::Result(Route)).new @cached_routes = Hash(String, Radix::Result(Route)).new
end end
@ -42,12 +44,20 @@ module Kemal
route route
end end
def lookup_route(request)
lookup_route request.override_method.as(String), request.path
end
def route_defined?(request)
lookup_route(request).found?
end
# Processes the route if it's a match. Otherwise renders 404. # Processes the route if it's a match. Otherwise renders 404.
private def process_request(context) private def process_request(context)
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_found? raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_found?
content = context.route.handler.call(context) content = context.route.handler.call(context)
if context.app.error_handlers.has_key?(context.response.status_code) if !app.error_handlers.empty? && app.error_handlers.has_key?(context.response.status_code)
raise Kemal::Exceptions::CustomException.new(context) raise Kemal::Exceptions::CustomException.new(context)
end end

View file

@ -4,13 +4,17 @@ module Kemal
property routes property routes
def initialize getter app : Kemal::Base
def initialize(@app)
@routes = Radix::Tree(WebSocket).new @routes = Radix::Tree(WebSocket).new
end end
def call(context : HTTP::Server::Context) def call(context : HTTP::Server::Context)
return call_next(context) unless context.ws_route_found? && websocket_upgrade_request?(context) route = lookup_ws_route(context.request.path)
content = context.websocket.call(context) return call_next(context) unless route.found? && websocket_upgrade_request?(context)
context.request.url_params ||= route.params
content = route.payload.call(context)
context.response.print(content) context.response.print(content)
context context
end end