Add more types to method signatures

This commit is contained in:
Serdar Dogruyol 2017-08-24 23:32:43 +03:00
parent efd97b75f9
commit 00217d9545
14 changed files with 34 additions and 34 deletions

View file

@ -8,7 +8,7 @@ require "./kemal/helpers/*"
module Kemal
# Overload of self.run with the default startup logging
def self.run(port = nil)
def self.run(port : Int32? = nil)
self.run port do
log "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}"
end
@ -22,7 +22,7 @@ module Kemal
# The command to run a `Kemal` application.
# The port can be given to `#run` but is optional.
# 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
config = Kemal.config
config.setup

View file

@ -4,6 +4,6 @@ module Kemal
include HTTP::Handler
abstract def call(context)
abstract def write(message)
abstract def write(message : String)
end
end

View file

@ -19,7 +19,7 @@ module Kemal
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)
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)

View file

@ -15,7 +15,7 @@ module Kemal
context
end
def write(message)
def write(message : String)
@handler << message
end

View file

@ -93,7 +93,7 @@ module Kemal
ERROR_HANDLERS
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 }
end

View file

@ -8,18 +8,18 @@ HTTP_METHODS = %w(get post put patch delete options)
FILTER_METHODS = %w(get post put patch delete options all)
{% 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)
Kemal::RouteHandler::INSTANCE.add_http_route({{method}}.upcase, path, &block)
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)
Kemal::WebSocketHandler.new path, &block
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
end
@ -28,7 +28,7 @@ end
# - after_all, after_get, after_post, after_put, after_patch, after_delete, after_options
{% for type in ["before", "after"] %}
{% 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)
end
{% end %}

View file

@ -27,7 +27,7 @@ class HTTP::Server
end
end
def redirect(url, status_code = 302)
def redirect(url : String, status_code : Int32 = 302)
@response.headers.add "Location", url
@response.status_code = status_code
end
@ -48,11 +48,11 @@ class HTTP::Server
ws_route_lookup.found?
end
def get(name)
def get(name : String)
@store[name]
end
def set(name, value)
def set(name : String, value : StoreTypes)
@store[name] = value
end
end

View file

@ -8,7 +8,7 @@ class HTTP::Request
end
# 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)
override_method = override_method.upcase
override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE"

View file

@ -63,7 +63,7 @@ module Kemal
@@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found?
end
private def radix_path(method : String, path)
private def radix_path(method : String, path : String)
"#{self.class}/#{method.downcase}#{path}"
end
end

View file

@ -8,23 +8,23 @@
# - Kemal::StaticFileHandler
# - Here goes custom handlers
# - Kemal::RouteHandler
def add_handler(handler)
def add_handler(handler : HTTP::Handler)
Kemal.config.add_handler handler
end
def add_handler(handler, position : Int32)
def add_handler(handler : HTTP::Handler, position : Int32)
Kemal.config.add_handler handler, position
end
# Sets public folder from which the static assets will be served.
# By default this is `/public` not `src/public`.
def public_folder(path)
def public_folder(path : String)
Kemal.config.public_folder = path
end
# Logs the output via `logger`.
# 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"
end
@ -32,7 +32,7 @@ end
# This is enabled by default.
#
# logging false
def logging(status)
def logging(status : Bool)
Kemal.config.logging = status
end
@ -57,7 +57,7 @@ end
# Now that we have a custom logger here's how we use it
#
# logger MyCustomLogger.new
def logger(logger)
def logger(logger : Kemal::BaseLogHandler)
Kemal.config.logger = logger
Kemal.config.add_handler logger
end
@ -81,7 +81,7 @@ end
# def call(env)
# headers(env, {"custom-header" => "This is a custom value"})
# end
def headers(env, additional_headers)
def headers(env : HTTP::Server::Context, additional_headers : Hash(String, String))
env.response.headers.merge!(additional_headers)
end

View file

@ -1,14 +1,14 @@
module Kemal
module Utils
def self.path_starts_with_slash?(path)
def self.path_starts_with_slash?(path : String)
path.starts_with?("/")
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)
end
def self.mime_type(path)
def self.mime_type(path : String)
case File.extname(path)
when ".txt" then "text/plain"
when ".htm", ".html" then "text/html"

View file

@ -1,11 +1,11 @@
module Kemal
# This is here to represent the logger corresponding to Null Object Pattern.
class NullLogHandler < Kemal::BaseLogHandler
def call(context)
def call(context : HTTP::Server::Context)
call_next(context)
end
def write(message)
def write(message : String)
end
end
end

View file

@ -7,7 +7,7 @@ module Kemal
@handler : HTTP::Server::Context -> 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.call(context).to_s
end

View file

@ -6,11 +6,11 @@ module Kemal
@context = OpenSSL::SSL::Context::Server.new
end
def key_file=(key_file)
def key_file=(key_file : String)
@context.private_key = key_file
end
def cert_file=(cert_file)
def cert_file=(cert_file : String)
@context.certificate_chain = cert_file
end
end