Use nested module declaration
This commit is contained in:
parent
4edebcf8eb
commit
f7484d14d3
9 changed files with 176 additions and 160 deletions
|
@ -1,12 +1,14 @@
|
||||||
# All loggers must inherit from `Kemal::BaseLogHandler`.
|
module Kemal
|
||||||
class Kemal::BaseLogHandler < HTTP::Handler
|
# All loggers must inherit from `Kemal::BaseLogHandler`.
|
||||||
def initialize
|
class Kemal::BaseLogHandler < HTTP::Handler
|
||||||
end
|
def initialize
|
||||||
|
end
|
||||||
|
|
||||||
def call(context)
|
def call(context)
|
||||||
call_next context
|
call_next context
|
||||||
end
|
end
|
||||||
|
|
||||||
def write(message)
|
def write(message)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,33 +1,35 @@
|
||||||
class Kemal::CommonLogHandler < Kemal::BaseLogHandler
|
module Kemal
|
||||||
@handler : IO::FileDescriptor
|
class CommonLogHandler < Kemal::BaseLogHandler
|
||||||
getter handler
|
@handler : IO::FileDescriptor
|
||||||
|
getter handler
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@handler = STDOUT
|
@handler = STDOUT
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(context)
|
def call(context)
|
||||||
time = Time.now
|
time = Time.now
|
||||||
call_next(context)
|
call_next(context)
|
||||||
elapsed_text = elapsed_text(Time.now - time)
|
elapsed_text = elapsed_text(Time.now - time)
|
||||||
@handler << time << " " << context.response.status_code << " " << context.request.method << " " << context.request.resource << " " << elapsed_text << "\n"
|
@handler << time << " " << context.response.status_code << " " << context.request.method << " " << context.request.resource << " " << elapsed_text << "\n"
|
||||||
context
|
context
|
||||||
end
|
end
|
||||||
|
|
||||||
def write(message)
|
def write(message)
|
||||||
@handler << message
|
@handler << message
|
||||||
end
|
end
|
||||||
|
|
||||||
private def elapsed_text(elapsed)
|
private def elapsed_text(elapsed)
|
||||||
minutes = elapsed.total_minutes
|
minutes = elapsed.total_minutes
|
||||||
return "#{minutes.round(2)}m" if minutes >= 1
|
return "#{minutes.round(2)}m" if minutes >= 1
|
||||||
|
|
||||||
seconds = elapsed.total_seconds
|
seconds = elapsed.total_seconds
|
||||||
return "#{seconds.round(2)}s" if seconds >= 1
|
return "#{seconds.round(2)}s" if seconds >= 1
|
||||||
|
|
||||||
millis = elapsed.total_milliseconds
|
millis = elapsed.total_milliseconds
|
||||||
return "#{millis.round(2)}ms" if millis >= 1
|
return "#{millis.round(2)}ms" if millis >= 1
|
||||||
|
|
||||||
"#{(millis * 1000).round(2)}µs"
|
"#{(millis * 1000).round(2)}µs"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
module Kemal::Middleware
|
module Kemal::Middleware
|
||||||
|
# This middleware adds SSL / TLS support.
|
||||||
class SSL
|
class SSL
|
||||||
getter context
|
getter context
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
# This is here to represent the logger corresponding to Null Object Pattern.
|
module Kemal
|
||||||
class Kemal::NullLogHandler < Kemal::BaseLogHandler
|
# This is here to represent the logger corresponding to Null Object Pattern.
|
||||||
|
class NullLogHandler < Kemal::BaseLogHandler
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,73 +1,75 @@
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
# ParamParser parses the request contents including query_params and body
|
module Kemal
|
||||||
# and converts them into a params hash which you can within the environment
|
# ParamParser parses the request contents including query_params and body
|
||||||
# context.
|
# and converts them into a params hash which you can within the environment
|
||||||
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)
|
# context.
|
||||||
|
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)
|
||||||
|
|
||||||
class Kemal::ParamParser
|
class ParamParser
|
||||||
URL_ENCODED_FORM = "application/x-www-form-urlencoded"
|
URL_ENCODED_FORM = "application/x-www-form-urlencoded"
|
||||||
APPLICATION_JSON = "application/json"
|
APPLICATION_JSON = "application/json"
|
||||||
|
|
||||||
def initialize(@request : HTTP::Request)
|
def initialize(@request : HTTP::Request)
|
||||||
@url = {} of String => String
|
@url = {} of String => String
|
||||||
@query = HTTP::Params.new({} of String => Array(String))
|
@query = HTTP::Params.new({} of String => Array(String))
|
||||||
@body = HTTP::Params.new({} of String => Array(String))
|
@body = HTTP::Params.new({} of String => Array(String))
|
||||||
@json = {} of String => AllParamTypes
|
@json = {} of String => AllParamTypes
|
||||||
@url_parsed = false
|
@url_parsed = false
|
||||||
@query_parsed = false
|
@query_parsed = false
|
||||||
@body_parsed = false
|
@body_parsed = false
|
||||||
@json_parsed = false
|
@json_parsed = false
|
||||||
end
|
end
|
||||||
|
|
||||||
{% for method in %w(url query body json) %}
|
{% for method in %w(url query body json) %}
|
||||||
def {{method.id}}
|
def {{method.id}}
|
||||||
# check memoization
|
# check memoization
|
||||||
return @{{method.id}} if @{{method.id}}_parsed
|
return @{{method.id}} if @{{method.id}}_parsed
|
||||||
|
|
||||||
parse_{{method.id}}
|
parse_{{method.id}}
|
||||||
# memoize
|
# memoize
|
||||||
@{{method.id}}_parsed = true
|
@{{method.id}}_parsed = true
|
||||||
@{{method.id}}
|
@{{method.id}}
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
def parse_body
|
def parse_body
|
||||||
return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil?
|
return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil?
|
||||||
@body = parse_part(@request.body)
|
@body = parse_part(@request.body)
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_query
|
def parse_query
|
||||||
@query = parse_part(@request.query)
|
@query = parse_part(@request.query)
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
# Parses JSON request body if Content-Type is `application/json`.
|
# Parses JSON request body if Content-Type is `application/json`.
|
||||||
# If request body is a JSON Hash then all the params are parsed and added into `params`.
|
# If request body is a JSON Hash then all the params are parsed and added into `params`.
|
||||||
# If request body is a JSON Array it's added into `params` as `_json` and can be accessed
|
# If request body is a JSON Array it's added into `params` as `_json` and can be accessed
|
||||||
# like params["_json"]
|
# like params["_json"]
|
||||||
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
|
||||||
|
when Array
|
||||||
|
@json["_json"] = json
|
||||||
end
|
end
|
||||||
when Array
|
|
||||||
@json["_json"] = json
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def parse_part(part)
|
def parse_part(part)
|
||||||
HTTP::Params.parse(part || "")
|
HTTP::Params.parse(part || "")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
# Route is the main building block of Kemal.
|
module Kemal
|
||||||
# It takes 3 parameters: Method, path and a block to specify
|
# Route is the main building block of Kemal.
|
||||||
# what action to be done if the route is matched.
|
# It takes 3 parameters: Method, path and a block to specify
|
||||||
class Kemal::Route
|
# what action to be done if the route is matched.
|
||||||
getter handler
|
class Route
|
||||||
@handler : HTTP::Server::Context -> String
|
getter handler
|
||||||
@method : String
|
@handler : HTTP::Server::Context -> String
|
||||||
|
@method : String
|
||||||
|
|
||||||
def initialize(@method, @path : String, &handler : HTTP::Server::Context -> _)
|
def initialize(@method, @path : String, &handler : HTTP::Server::Context -> _)
|
||||||
@handler = ->(context : HTTP::Server::Context) { handler.call(context).to_s }
|
@handler = ->(context : HTTP::Server::Context) { handler.call(context).to_s }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,53 +1,54 @@
|
||||||
require "http/server"
|
|
||||||
require "radix"
|
require "radix"
|
||||||
|
|
||||||
# Kemal::RouteHandler is the main handler which handles all the HTTP requests. Routing, parsing, rendering e.g
|
module Kemal
|
||||||
# are done in this handler.
|
# Kemal::RouteHandler is the main handler which handles all the HTTP requests. Routing, parsing, rendering e.g
|
||||||
class Kemal::RouteHandler < HTTP::Handler
|
# are done in this handler.
|
||||||
INSTANCE = new
|
class RouteHandler < HTTP::Handler
|
||||||
|
INSTANCE = new
|
||||||
|
|
||||||
property tree
|
property tree
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@tree = Radix::Tree(Route).new
|
@tree = Radix::Tree(Route).new
|
||||||
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
|
|
||||||
|
|
||||||
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
|
|
||||||
# a corresponding `HEAD` route.
|
|
||||||
def add_route(method, path, &handler : HTTP::Server::Context -> _)
|
|
||||||
add_to_radix_tree method, path, Route.new(method, path, &handler)
|
|
||||||
add_to_radix_tree("HEAD", path, Route.new("HEAD", path, &handler)) if method == "GET"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Check if a route is defined and returns the lookup
|
|
||||||
def lookup_route(verb, path)
|
|
||||||
@tree.find radix_path(verb, path)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Processes the route if it's a match. Otherwise renders 404.
|
|
||||||
def process_request(context)
|
|
||||||
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined?
|
|
||||||
route = context.route_lookup.payload.as(Route)
|
|
||||||
content = route.handler.call(context)
|
|
||||||
if Kemal.config.error_handlers.has_key?(context.response.status_code)
|
|
||||||
raise Kemal::Exceptions::CustomException.new(context)
|
|
||||||
end
|
end
|
||||||
context.response.print(content)
|
|
||||||
context
|
|
||||||
end
|
|
||||||
|
|
||||||
private def radix_path(method : String, path)
|
def call(context)
|
||||||
"/#{method.downcase}#{path}"
|
context.response.headers.add "X-Powered-By", "Kemal"
|
||||||
end
|
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
|
||||||
|
process_request(context)
|
||||||
|
end
|
||||||
|
|
||||||
private def add_to_radix_tree(method, path, route)
|
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
|
||||||
node = radix_path method, path
|
# a corresponding `HEAD` route.
|
||||||
@tree.add node, route
|
def add_route(method, path, &handler : HTTP::Server::Context -> _)
|
||||||
|
add_to_radix_tree method, path, Route.new(method, path, &handler)
|
||||||
|
add_to_radix_tree("HEAD", path, Route.new("HEAD", path, &handler)) if method == "GET"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Check if a route is defined and returns the lookup
|
||||||
|
def lookup_route(verb, path)
|
||||||
|
@tree.find radix_path(verb, path)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Processes the route if it's a match. Otherwise renders 404.
|
||||||
|
def process_request(context)
|
||||||
|
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined?
|
||||||
|
route = context.route_lookup.payload.as(Route)
|
||||||
|
content = route.handler.call(context)
|
||||||
|
if Kemal.config.error_handlers.has_key?(context.response.status_code)
|
||||||
|
raise Kemal::Exceptions::CustomException.new(context)
|
||||||
|
end
|
||||||
|
context.response.print(content)
|
||||||
|
context
|
||||||
|
end
|
||||||
|
|
||||||
|
private def radix_path(method : String, path)
|
||||||
|
"/#{method.downcase}#{path}"
|
||||||
|
end
|
||||||
|
|
||||||
|
private def add_to_radix_tree(method, path, route)
|
||||||
|
node = radix_path method, path
|
||||||
|
@tree.add node, route
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
# Kemal::StaticFileHandler is used to serve static files(.js/.css/.png e.g).
|
module Kemal
|
||||||
# This handler is on by default and you can disable it like.
|
# Kemal::StaticFileHandler is used to serve static files(.js/.css/.png e.g).
|
||||||
#
|
# This handler is on by default and you can disable it like.
|
||||||
# serve_static false
|
#
|
||||||
#
|
# serve_static false
|
||||||
class Kemal::StaticFileHandler < HTTP::StaticFileHandler
|
#
|
||||||
def call(context)
|
class StaticFileHandler < HTTP::StaticFileHandler
|
||||||
return call_next(context) if context.request.path.not_nil! == "/"
|
def call(context)
|
||||||
super
|
return call_next(context) if context.request.path.not_nil! == "/"
|
||||||
|
super
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
# Kemal::WebSocketHandler is used for building a WebSocket route.
|
module Kemal
|
||||||
# For each WebSocket route a new handler is created and registered to global handlers.
|
# Kemal::WebSocketHandler is used for building a WebSocket route.
|
||||||
class Kemal::WebSocketHandler < HTTP::WebSocketHandler
|
# For each WebSocket route a new handler is created and registered to global handlers.
|
||||||
def initialize(@path : String, &@proc : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
class WebSocketHandler < HTTP::WebSocketHandler
|
||||||
Kemal.config.add_ws_handler self
|
def initialize(@path : String, &@proc : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
||||||
end
|
Kemal.config.add_ws_handler self
|
||||||
|
end
|
||||||
|
|
||||||
def call(context)
|
def call(context)
|
||||||
return call_next(context) unless context.request.path.not_nil! == @path
|
return call_next(context) unless context.request.path.not_nil! == @path
|
||||||
super
|
super
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue