mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Introduce Kemal::InitHandler to initialize HTTP::Server::Context with defaults
This commit is contained in:
parent
725e051723
commit
09d9e708f1
10 changed files with 48 additions and 40 deletions
|
@ -7,12 +7,10 @@ module Kemal
|
|||
begin
|
||||
call_next(context)
|
||||
rescue Kemal::Exceptions::RouteNotFound
|
||||
context.response.content_type = "text/html"
|
||||
call_exception_with_status_code(context, 404)
|
||||
rescue Kemal::Exceptions::CustomException
|
||||
call_exception_with_status_code(context, context.response.status_code)
|
||||
rescue ex : Exception
|
||||
context.response.content_type = "text/html"
|
||||
Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace}\n")
|
||||
return call_exception_with_status_code(context, 500) if Kemal.config.error_handlers.has_key?(500)
|
||||
verbosity = Kemal.config.env == "production" ? false : true
|
||||
|
|
|
@ -58,33 +58,38 @@ module Kemal
|
|||
ERROR_HANDLERS[status_code] = ->(context : HTTP::Server::Context) { handler.call(context).to_s }
|
||||
end
|
||||
|
||||
def extra_options(&@extra_options : OptionParser ->)
|
||||
end
|
||||
|
||||
def setup
|
||||
setup_init_handler
|
||||
setup_log_handler
|
||||
setup_error_handler
|
||||
setup_static_file_handler
|
||||
end
|
||||
|
||||
def setup_log_handler
|
||||
private def setup_init_handler
|
||||
HANDLERS.insert(0, Kemal::InitHandler::INSTANCE)
|
||||
end
|
||||
|
||||
private def setup_log_handler
|
||||
@logger ||= if @logging
|
||||
Kemal::CommonLogHandler.new
|
||||
else
|
||||
Kemal::NullLogHandler.new
|
||||
end
|
||||
HANDLERS.insert(0, @logger.not_nil!)
|
||||
end
|
||||
|
||||
def extra_options(&@extra_options : OptionParser ->)
|
||||
HANDLERS.insert(1, @logger.not_nil!)
|
||||
end
|
||||
|
||||
private def setup_error_handler
|
||||
if @always_rescue
|
||||
@error_handler ||= Kemal::CommonExceptionHandler.new
|
||||
HANDLERS.insert(1, @error_handler.not_nil!)
|
||||
HANDLERS.insert(2, @error_handler.not_nil!)
|
||||
end
|
||||
end
|
||||
|
||||
private def setup_static_file_handler
|
||||
HANDLERS.insert(2, Kemal::StaticFileHandler.new(@public_folder)) if @serve_static
|
||||
HANDLERS.insert(3, Kemal::StaticFileHandler.new(@public_folder)) if @serve_static
|
||||
end
|
||||
end
|
||||
|
||||
|
|
12
src/kemal/init_handler.cr
Normal file
12
src/kemal/init_handler.cr
Normal file
|
@ -0,0 +1,12 @@
|
|||
module Kemal
|
||||
# Kemal::InitHandler is the first handler thus initializes the context with default values such as
|
||||
# Content-Type, X-Powered-By.
|
||||
class InitHandler < HTTP::Handler
|
||||
INSTANCE = new
|
||||
|
||||
def call(context)
|
||||
context.response.content_type = "text/html"
|
||||
context.response.headers.add "X-Powered-By", "Kemal"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -13,8 +13,6 @@ module Kemal
|
|||
end
|
||||
|
||||
def call(context)
|
||||
context.response.headers.add "X-Powered-By", "Kemal"
|
||||
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
|
||||
process_request(context)
|
||||
end
|
||||
|
||||
|
|
|
@ -48,7 +48,6 @@ def render_500(context, backtrace, verbosity)
|
|||
</body>
|
||||
</html>
|
||||
HTML
|
||||
context.response.content_type = "text/html"
|
||||
context.response.status_code = 500
|
||||
context.response.print template
|
||||
context
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue