Updating to instance types

This commit is contained in:
sdogruyol 2016-04-12 14:41:09 +03:00
parent de98236fc4
commit 05e44e68c6
15 changed files with 27 additions and 18 deletions

View file

@ -20,7 +20,7 @@ describe "Kemal::RouteHandler" do
end end
it "routes request with multiple query strings" do it "routes request with multiple query strings" do
get "/" do |env| get "/" do |env|
"hello #{env.params.query["message"]} time #{env.params.query["time"]}" "hello #{env.params.query["message"]} time #{env.params.query["time"]}"
end end
request = HTTP::Request.new("GET", "/?message=world&time=now") request = HTTP::Request.new("GET", "/?message=world&time=now")
@ -155,5 +155,4 @@ describe "Kemal::RouteHandler" do
client_response.status_code.should eq(302) client_response.status_code.should eq(302)
client_response.headers.has_key?("Location").should eq(true) client_response.headers.has_key?("Location").should eq(true)
end end
end end

View file

@ -53,5 +53,5 @@ end
Spec.after_each do Spec.after_each do
Kemal.config.handlers.clear Kemal.config.handlers.clear
Kemal::RouteHandler::INSTANCE.tree = Radix::Tree.new Kemal::RouteHandler::INSTANCE.tree = Radix::Tree(Route).new
end end

View file

@ -1,7 +1,7 @@
require "http" require "http"
class Kemal::BaseLogHandler < HTTP::Handler class Kemal::BaseLogHandler < HTTP::Handler
def initialize(@env) def initialize(@env : String)
end end
def call(context) def call(context)

View file

@ -2,10 +2,13 @@ require "option_parser"
module Kemal module Kemal
class CLI class CLI
@config : Kemal::Config
@key_file : String
def initialize def initialize
@ssl_enabled = false @ssl_enabled = false
@key_file = nil @key_file = ""
@cert_file = nil @cert_file = ""
@config = Kemal.config @config = Kemal.config
parse parse
configure_ssl configure_ssl

View file

@ -2,6 +2,7 @@ require "colorize"
require "http" require "http"
class Kemal::CommonLogHandler < Kemal::BaseLogHandler class Kemal::CommonLogHandler < Kemal::BaseLogHandler
@handler : IO::FileDescriptor+
getter handler getter handler
def initialize(@env) def initialize(@env)

View file

@ -2,6 +2,8 @@ module Kemal
class Config class Config
INSTANCE = Config.new INSTANCE = Config.new
HANDLERS = [] of HTTP::Handler HANDLERS = [] of HTTP::Handler
@ssl : OpenSSL::SSL::Context
property host_binding, ssl, port, env, public_folder, logging, property host_binding, ssl, port, env, public_folder, logging,
always_rescue, error_handler, serve_static, run always_rescue, error_handler, serve_static, run
@ -14,8 +16,9 @@ module Kemal
@logging = true @logging = true
@logger = nil @logger = nil
@always_rescue = true @always_rescue = true
@error_handler = nil @error_handler = uninitialized Kemal::CommonExceptionHandler
@run = false @run = false
@ssl = uninitialized OpenSSL::SSL::Context
end end
def logger def logger

View file

@ -13,7 +13,7 @@ class HTTP::Server
end end
def route_lookup def route_lookup
@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?

View file

@ -52,7 +52,6 @@ def logger(logger)
Kemal.config.add_handler logger Kemal.config.add_handler logger
end end
def serve_static(status) def serve_static(status)
Kemal.config.serve_static = status Kemal.config.serve_static = status
end end

View file

@ -6,7 +6,7 @@ module Kemal::Middleware
# This middleware is lazily instantiated and added to the handlers as soon as a call to `after_X` or `before_X` is made. # This middleware is lazily instantiated and added to the handlers as soon as a call to `after_X` or `before_X` is made.
def initialize def initialize
@tree = Radix::Tree.new @tree = Radix::Tree(Array(Kemal::Middleware::Block)).new
Kemal.config.add_handler(self) Kemal.config.add_handler(self)
end end
@ -68,7 +68,7 @@ module Kemal::Middleware
end end
class Block class Block
property block property block : (HTTP::Server::Context -> )
def initialize(&@block : HTTP::Server::Context -> _) def initialize(&@block : HTTP::Server::Context -> _)
end end

View file

@ -13,7 +13,7 @@ module Kemal::Middleware
AUTH_MESSAGE = "Could not verify your access level for that URL.\nYou have to login with proper credentials" AUTH_MESSAGE = "Could not verify your access level for that URL.\nYou have to login with proper credentials"
HEADER_LOGIN_REQUIRED = "Basic realm=\"Login Required\"" HEADER_LOGIN_REQUIRED = "Basic realm=\"Login Required\""
def initialize(@username, @password) def initialize(@username : String?, @password : String?)
end end
def call(context) def call(context)

View file

@ -9,11 +9,15 @@ class Kemal::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) def initialize(@request : HTTP::Request, @url_params : String? = nil , @override_method : String? = nil)
@url = {} of String => String @url = {} of String => String
@query = {} of String => String @query = {} of String => String
@body = {} of String => String @body = {} of String => String
@json = {} of String => AllParamTypes @json = {} of String => AllParamTypes
@url_parsed = false
@query_parsed = false
@body_parsed = false
@json_parsed = false
end end
{% for method in %w(url query body json) %} {% for method in %w(url query body json) %}

View file

@ -1,7 +1,7 @@
# Opening HTTP::Request to add override_method property # Opening HTTP::Request to add override_method property
class HTTP::Request class HTTP::Request
property override_method property override_method
property url_params property url_params : Hash(String, String)?
def override_method def override_method
@override_method ||= check_for_method_override! @override_method ||= check_for_method_override!

View file

@ -2,9 +2,9 @@
# It takes 3 parameters: Method, path and a block to specify # It takes 3 parameters: Method, path and a block to specify
# what action to be done if the route is matched. # what action to be done if the route is matched.
class Kemal::Route class Kemal::Route
getter handler getter handler : (HTTP::Server::Context -> )
getter method getter method
def initialize(@method, @path, &@handler : HTTP::Server::Context -> _) def initialize(@method : String, @path : String, &@handler : HTTP::Server::Context -> )
end end
end end

View file

@ -9,7 +9,7 @@ class Kemal::RouteHandler < HTTP::Handler
property tree property tree
def initialize def initialize
@tree = Radix::Tree.new @tree = Radix::Tree(Route).new
end end
def call(context) def call(context)

View file

@ -1,7 +1,7 @@
# Kemal::WebSocketHandler is used for each define WebSocket route. # Kemal::WebSocketHandler is used for each define WebSocket route.
# For each WebSocket route a new handler is created and registered to global handlers. # For each WebSocket route a new handler is created and registered to global handlers.
class Kemal::WebSocketHandler < HTTP::WebSocketHandler class Kemal::WebSocketHandler < HTTP::WebSocketHandler
def initialize(@path, &@proc : HTTP::WebSocket, HTTP::Server::Context -> Void) def initialize(@path : String, &@proc : HTTP::WebSocket, HTTP::Server::Context -> Void)
Kemal.config.add_ws_handler self Kemal.config.add_ws_handler self
end end