Add more types to method signatures
This commit is contained in:
parent
efd97b75f9
commit
00217d9545
14 changed files with 34 additions and 34 deletions
|
@ -8,7 +8,7 @@ require "./kemal/helpers/*"
|
||||||
|
|
||||||
module Kemal
|
module Kemal
|
||||||
# Overload of self.run with the default startup logging
|
# Overload of self.run with the default startup logging
|
||||||
def self.run(port = nil)
|
def self.run(port : Int32? = nil)
|
||||||
self.run port do
|
self.run port do
|
||||||
log "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}"
|
log "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}"
|
||||||
end
|
end
|
||||||
|
@ -22,7 +22,7 @@ module Kemal
|
||||||
# The command to run a `Kemal` application.
|
# The command to run a `Kemal` application.
|
||||||
# The port can be given to `#run` but is optional.
|
# The port can be given to `#run` but is optional.
|
||||||
# If not given Kemal will use `Kemal::Config#port`
|
# If not given Kemal will use `Kemal::Config#port`
|
||||||
def self.run(port = nil, &block)
|
def self.run(port : Int32? = nil, &block)
|
||||||
Kemal::CLI.new
|
Kemal::CLI.new
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
config.setup
|
config.setup
|
||||||
|
|
|
@ -4,6 +4,6 @@ module Kemal
|
||||||
include HTTP::Handler
|
include HTTP::Handler
|
||||||
|
|
||||||
abstract def call(context)
|
abstract def call(context)
|
||||||
abstract def write(message)
|
abstract def write(message : String)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,7 +19,7 @@ module Kemal
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def call_exception_with_status_code(context, exception, status_code)
|
private def call_exception_with_status_code(context : HTTP::Server::Context, exception : Exception, status_code : Int32)
|
||||||
if Kemal.config.error_handlers.has_key?(status_code)
|
if Kemal.config.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 Kemal.config.error_handlers[status_code].call(context, exception)
|
context.response.print Kemal.config.error_handlers[status_code].call(context, exception)
|
||||||
|
|
|
@ -15,7 +15,7 @@ module Kemal
|
||||||
context
|
context
|
||||||
end
|
end
|
||||||
|
|
||||||
def write(message)
|
def write(message : String)
|
||||||
@handler << message
|
@handler << message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -93,7 +93,7 @@ module Kemal
|
||||||
ERROR_HANDLERS
|
ERROR_HANDLERS
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_error_handler(status_code, &handler : HTTP::Server::Context, Exception -> _)
|
def add_error_handler(status_code : Int32, &handler : HTTP::Server::Context, Exception -> _)
|
||||||
ERROR_HANDLERS[status_code] = ->(context : HTTP::Server::Context, error : Exception) { handler.call(context, error).to_s }
|
ERROR_HANDLERS[status_code] = ->(context : HTTP::Server::Context, error : Exception) { handler.call(context, error).to_s }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -8,18 +8,18 @@ HTTP_METHODS = %w(get post put patch delete options)
|
||||||
FILTER_METHODS = %w(get post put patch delete options all)
|
FILTER_METHODS = %w(get post put patch delete options all)
|
||||||
|
|
||||||
{% for method in HTTP_METHODS %}
|
{% for method in HTTP_METHODS %}
|
||||||
def {{method.id}}(path, &block : HTTP::Server::Context -> _)
|
def {{method.id}}(path : String, &block : HTTP::Server::Context -> _)
|
||||||
raise Kemal::Exceptions::InvalidPathStartException.new({{method}}, path) unless Kemal::Utils.path_starts_with_slash?(path)
|
raise Kemal::Exceptions::InvalidPathStartException.new({{method}}, path) unless Kemal::Utils.path_starts_with_slash?(path)
|
||||||
Kemal::RouteHandler::INSTANCE.add_http_route({{method}}.upcase, path, &block)
|
Kemal::RouteHandler::INSTANCE.add_http_route({{method}}.upcase, path, &block)
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
def ws(path, &block : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
def ws(path : String, &block : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
||||||
raise Kemal::Exceptions::InvalidPathStartException.new("ws", path) unless Kemal::Utils.path_starts_with_slash?(path)
|
raise Kemal::Exceptions::InvalidPathStartException.new("ws", path) unless Kemal::Utils.path_starts_with_slash?(path)
|
||||||
Kemal::WebSocketHandler.new path, &block
|
Kemal::WebSocketHandler.new path, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
def error(status_code, &block : HTTP::Server::Context, Exception -> _)
|
def error(status_code : Int32, &block : HTTP::Server::Context, Exception -> _)
|
||||||
Kemal.config.add_error_handler status_code, &block
|
Kemal.config.add_error_handler status_code, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ end
|
||||||
# - after_all, after_get, after_post, after_put, after_patch, after_delete, after_options
|
# - after_all, after_get, after_post, after_put, after_patch, after_delete, after_options
|
||||||
{% for type in ["before", "after"] %}
|
{% for type in ["before", "after"] %}
|
||||||
{% for method in FILTER_METHODS %}
|
{% for method in FILTER_METHODS %}
|
||||||
def {{type.id}}_{{method.id}}(path = "*", &block : HTTP::Server::Context -> _)
|
def {{type.id}}_{{method.id}}(path : String = "*", &block : HTTP::Server::Context -> _)
|
||||||
Kemal::FilterHandler::INSTANCE.{{type.id}}({{method}}.upcase, path, &block)
|
Kemal::FilterHandler::INSTANCE.{{type.id}}({{method}}.upcase, path, &block)
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
|
@ -27,7 +27,7 @@ class HTTP::Server
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def redirect(url, status_code = 302)
|
def redirect(url : String, status_code : Int32 = 302)
|
||||||
@response.headers.add "Location", url
|
@response.headers.add "Location", url
|
||||||
@response.status_code = status_code
|
@response.status_code = status_code
|
||||||
end
|
end
|
||||||
|
@ -48,11 +48,11 @@ class HTTP::Server
|
||||||
ws_route_lookup.found?
|
ws_route_lookup.found?
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(name)
|
def get(name : String)
|
||||||
@store[name]
|
@store[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(name, value)
|
def set(name : String, value : StoreTypes)
|
||||||
@store[name] = value
|
@store[name] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ class HTTP::Request
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks if method contained in _method param is valid one
|
# Checks if method contained in _method param is valid one
|
||||||
def self.override_method_valid?(override_method)
|
def self.override_method_valid?(override_method : String)
|
||||||
return false unless override_method.is_a?(String)
|
return false unless override_method.is_a?(String)
|
||||||
override_method = override_method.upcase
|
override_method = override_method.upcase
|
||||||
override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE"
|
override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE"
|
||||||
|
|
|
@ -63,7 +63,7 @@ module Kemal
|
||||||
@@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found?
|
@@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found?
|
||||||
end
|
end
|
||||||
|
|
||||||
private def radix_path(method : String, path)
|
private def radix_path(method : String, path : String)
|
||||||
"#{self.class}/#{method.downcase}#{path}"
|
"#{self.class}/#{method.downcase}#{path}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,23 +8,23 @@
|
||||||
# - Kemal::StaticFileHandler
|
# - Kemal::StaticFileHandler
|
||||||
# - Here goes custom handlers
|
# - Here goes custom handlers
|
||||||
# - Kemal::RouteHandler
|
# - Kemal::RouteHandler
|
||||||
def add_handler(handler)
|
def add_handler(handler : HTTP::Handler)
|
||||||
Kemal.config.add_handler handler
|
Kemal.config.add_handler handler
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_handler(handler, position : Int32)
|
def add_handler(handler : HTTP::Handler, position : Int32)
|
||||||
Kemal.config.add_handler handler, position
|
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.
|
||||||
# By default this is `/public` not `src/public`.
|
# By default this is `/public` not `src/public`.
|
||||||
def public_folder(path)
|
def public_folder(path : String)
|
||||||
Kemal.config.public_folder = path
|
Kemal.config.public_folder = path
|
||||||
end
|
end
|
||||||
|
|
||||||
# Logs the output via `logger`.
|
# Logs the output via `logger`.
|
||||||
# This is the built-in `Kemal::CommonLogHandler` by default which uses STDOUT.
|
# This is the built-in `Kemal::CommonLogHandler` by default which uses STDOUT.
|
||||||
def log(message)
|
def log(message : String)
|
||||||
Kemal.config.logger.write "#{message}\n"
|
Kemal.config.logger.write "#{message}\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ end
|
||||||
# This is enabled by default.
|
# This is enabled by default.
|
||||||
#
|
#
|
||||||
# logging false
|
# logging false
|
||||||
def logging(status)
|
def logging(status : Bool)
|
||||||
Kemal.config.logging = status
|
Kemal.config.logging = status
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ end
|
||||||
# Now that we have a custom logger here's how we use it
|
# Now that we have a custom logger here's how we use it
|
||||||
#
|
#
|
||||||
# logger MyCustomLogger.new
|
# logger MyCustomLogger.new
|
||||||
def logger(logger)
|
def logger(logger : Kemal::BaseLogHandler)
|
||||||
Kemal.config.logger = logger
|
Kemal.config.logger = logger
|
||||||
Kemal.config.add_handler logger
|
Kemal.config.add_handler logger
|
||||||
end
|
end
|
||||||
|
@ -81,7 +81,7 @@ end
|
||||||
# def call(env)
|
# def call(env)
|
||||||
# headers(env, {"custom-header" => "This is a custom value"})
|
# headers(env, {"custom-header" => "This is a custom value"})
|
||||||
# end
|
# end
|
||||||
def headers(env, additional_headers)
|
def headers(env : HTTP::Server::Context, additional_headers : Hash(String, String))
|
||||||
env.response.headers.merge!(additional_headers)
|
env.response.headers.merge!(additional_headers)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
module Kemal
|
module Kemal
|
||||||
module Utils
|
module Utils
|
||||||
def self.path_starts_with_slash?(path)
|
def self.path_starts_with_slash?(path : String)
|
||||||
path.starts_with?("/")
|
path.starts_with?("/")
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.zip_types(path) # https://github.com/h5bp/server-configs-nginx/blob/master/nginx.conf
|
def self.zip_types(path : String) # https://github.com/h5bp/server-configs-nginx/blob/master/nginx.conf
|
||||||
[".htm", ".html", ".txt", ".css", ".js", ".svg", ".json", ".xml", ".otf", ".ttf", ".woff", ".woff2"].includes? File.extname(path)
|
[".htm", ".html", ".txt", ".css", ".js", ".svg", ".json", ".xml", ".otf", ".ttf", ".woff", ".woff2"].includes? File.extname(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.mime_type(path)
|
def self.mime_type(path : String)
|
||||||
case File.extname(path)
|
case File.extname(path)
|
||||||
when ".txt" then "text/plain"
|
when ".txt" then "text/plain"
|
||||||
when ".htm", ".html" then "text/html"
|
when ".htm", ".html" then "text/html"
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
module Kemal
|
module Kemal
|
||||||
# This is here to represent the logger corresponding to Null Object Pattern.
|
# This is here to represent the logger corresponding to Null Object Pattern.
|
||||||
class NullLogHandler < Kemal::BaseLogHandler
|
class NullLogHandler < Kemal::BaseLogHandler
|
||||||
def call(context)
|
def call(context : HTTP::Server::Context)
|
||||||
call_next(context)
|
call_next(context)
|
||||||
end
|
end
|
||||||
|
|
||||||
def write(message)
|
def write(message : String)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,7 +7,7 @@ module Kemal
|
||||||
@handler : HTTP::Server::Context -> String
|
@handler : HTTP::Server::Context -> String
|
||||||
@method : String
|
@method : String
|
||||||
|
|
||||||
def initialize(@method, @path : String, &handler : HTTP::Server::Context -> _)
|
def initialize(@method : String, @path : String, &handler : HTTP::Server::Context -> _)
|
||||||
@handler = ->(context : HTTP::Server::Context) do
|
@handler = ->(context : HTTP::Server::Context) do
|
||||||
handler.call(context).to_s
|
handler.call(context).to_s
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,11 +6,11 @@ module Kemal
|
||||||
@context = OpenSSL::SSL::Context::Server.new
|
@context = OpenSSL::SSL::Context::Server.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def key_file=(key_file)
|
def key_file=(key_file : String)
|
||||||
@context.private_key = key_file
|
@context.private_key = key_file
|
||||||
end
|
end
|
||||||
|
|
||||||
def cert_file=(cert_file)
|
def cert_file=(cert_file : String)
|
||||||
@context.certificate_chain = cert_file
|
@context.certificate_chain = cert_file
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue