Format
This commit is contained in:
parent
e9665c124b
commit
f92d812c32
|
@ -3,7 +3,6 @@ require "option_parser"
|
||||||
module Kemal
|
module Kemal
|
||||||
# Handles all the initialization from the command line.
|
# Handles all the initialization from the command line.
|
||||||
class CLI
|
class CLI
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@ssl_enabled = false
|
@ssl_enabled = false
|
||||||
@key_file = ""
|
@key_file = ""
|
||||||
|
|
|
@ -13,7 +13,7 @@ class HTTP::Server
|
||||||
end
|
end
|
||||||
|
|
||||||
def route_lookup
|
def route_lookup
|
||||||
Kemal::RouteHandler::INSTANCE.lookup_route(@request.override_method as String, @request.path)
|
Kemal::RouteHandler::INSTANCE.lookup_route(@request.override_method.as(String), @request.path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def route_defined?
|
def route_defined?
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
module Kemal::Exceptions
|
module Kemal::Exceptions
|
||||||
class RouteNotFound < Exception
|
class RouteNotFound < Exception
|
||||||
def initialize(context)
|
def initialize(context)
|
||||||
super "Requested path: '#{context.request.override_method as String}:#{context.request.path}' was not found."
|
super "Requested path: '#{context.request.override_method.as(String)}:#{context.request.path}' was not found."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class CustomException < Exception
|
class CustomException < Exception
|
||||||
|
|
||||||
def initialize(context)
|
def initialize(context)
|
||||||
super "Rendered error with #{context.response.status_code}"
|
super "Rendered error with #{context.response.status_code}"
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,7 +29,7 @@ module Kemal::Middleware
|
||||||
def _add_route_filter(verb, path, type, &block : HTTP::Server::Context -> _)
|
def _add_route_filter(verb, path, type, &block : HTTP::Server::Context -> _)
|
||||||
lookup = lookup_filters_for_path_type(verb, path, type)
|
lookup = lookup_filters_for_path_type(verb, path, type)
|
||||||
if lookup.found? && lookup.payload.is_a?(Array(Block))
|
if lookup.found? && lookup.payload.is_a?(Array(Block))
|
||||||
(lookup.payload as Array(Block)) << Block.new(&block)
|
(lookup.payload.as(Array(Block))) << Block.new(&block)
|
||||||
else
|
else
|
||||||
@tree.add radix_path(verb, path, type), [Block.new(&block)]
|
@tree.add radix_path(verb, path, type), [Block.new(&block)]
|
||||||
end
|
end
|
||||||
|
@ -49,7 +49,7 @@ module Kemal::Middleware
|
||||||
private def call_block_for_path_type(verb, path, type, context)
|
private def call_block_for_path_type(verb, path, type, context)
|
||||||
lookup = lookup_filters_for_path_type(verb, path, type)
|
lookup = lookup_filters_for_path_type(verb, path, type)
|
||||||
if lookup.found? && lookup.payload.is_a? Array(Block)
|
if lookup.found? && lookup.payload.is_a? Array(Block)
|
||||||
blocks = lookup.payload as Array(Block)
|
blocks = lookup.payload.as(Array(Block))
|
||||||
blocks.each { |block| block.call(context) }
|
blocks.each { |block| block.call(context) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -44,7 +44,7 @@ class Kemal::ParamParser
|
||||||
def parse_url
|
def parse_url
|
||||||
if params = @request.url_params
|
if params = @request.url_params
|
||||||
params.each do |key, value|
|
params.each do |key, value|
|
||||||
@url[key as String] = value as String
|
@url[key.as(String)] = value.as(String)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -56,11 +56,11 @@ class Kemal::ParamParser
|
||||||
def parse_json
|
def parse_json
|
||||||
return unless @request.body && @request.headers["Content-Type"]? == APPLICATION_JSON
|
return unless @request.body && @request.headers["Content-Type"]? == APPLICATION_JSON
|
||||||
|
|
||||||
body = @request.body as String
|
body = @request.body.as(String)
|
||||||
case json = JSON.parse(body).raw
|
case json = JSON.parse(body).raw
|
||||||
when Hash
|
when Hash
|
||||||
json.each do |key, value|
|
json.each do |key, value|
|
||||||
@json[key as String] = value as AllParamTypes
|
@json[key.as(String)] = value.as(AllParamTypes)
|
||||||
end
|
end
|
||||||
when Array
|
when Array
|
||||||
@json["_json"] = json
|
@json["_json"] = json
|
||||||
|
|
|
@ -32,7 +32,7 @@ class Kemal::RouteHandler < HTTP::Handler
|
||||||
# Processes the route if it's a match. Otherwise renders 404.
|
# Processes the route if it's a match. Otherwise renders 404.
|
||||||
def process_request(context)
|
def process_request(context)
|
||||||
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined?
|
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined?
|
||||||
route = context.route_lookup.payload as Route
|
route = context.route_lookup.payload.as(Route)
|
||||||
content = route.handler.call(context)
|
content = route.handler.call(context)
|
||||||
if Kemal.config.error_handlers.has_key?(context.response.status_code)
|
if Kemal.config.error_handlers.has_key?(context.response.status_code)
|
||||||
raise Kemal::Exceptions::CustomException.new(context)
|
raise Kemal::Exceptions::CustomException.new(context)
|
||||||
|
|
Loading…
Reference in New Issue