mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Merge branch 'instance-vars'
This commit is contained in:
commit
a8ecbde222
18 changed files with 48 additions and 36 deletions
|
@ -4,7 +4,7 @@ version: 0.11.1
|
||||||
dependencies:
|
dependencies:
|
||||||
radix:
|
radix:
|
||||||
github: luislavena/radix
|
github: luislavena/radix
|
||||||
version: 0.1.1
|
version: 0.3.0
|
||||||
kilt:
|
kilt:
|
||||||
github: jeromegn/kilt
|
github: jeromegn/kilt
|
||||||
version: 0.3.3
|
version: 0.3.3
|
||||||
|
|
|
@ -186,5 +186,5 @@ describe "Kemal::Middleware::Filters" do
|
||||||
end
|
end
|
||||||
|
|
||||||
class FilterTest
|
class FilterTest
|
||||||
property modified
|
property modified : String?
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -9,12 +9,12 @@ module Kemal
|
||||||
config.add_handler Kemal::RouteHandler::INSTANCE
|
config.add_handler Kemal::RouteHandler::INSTANCE
|
||||||
|
|
||||||
config.server = HTTP::Server.new(config.host_binding.not_nil!, config.port, config.handlers)
|
config.server = HTTP::Server.new(config.host_binding.not_nil!, config.port, config.handlers)
|
||||||
config.server.ssl = config.ssl
|
config.server.not_nil!.ssl = config.ssl
|
||||||
|
|
||||||
unless config.env == "test"
|
unless config.env == "test"
|
||||||
Signal::INT.trap {
|
Signal::INT.trap {
|
||||||
config.logger.write "Kemal is going to take a rest!\n"
|
config.logger.write "Kemal is going to take a rest!\n"
|
||||||
config.server.close
|
config.server.not_nil!.close
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ module Kemal
|
||||||
end
|
end
|
||||||
|
|
||||||
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
|
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
|
||||||
config.server.listen
|
config.server.not_nil!.listen
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -2,9 +2,11 @@ module Kemal
|
||||||
class Config
|
class Config
|
||||||
INSTANCE = Config.new
|
INSTANCE = Config.new
|
||||||
HANDLERS = [] of HTTP::Handler
|
HANDLERS = [] of HTTP::Handler
|
||||||
|
@ssl : OpenSSL::SSL::Context?
|
||||||
|
@server : HTTP::Server?
|
||||||
|
|
||||||
property host_binding, ssl, port, env, public_folder, logging,
|
property host_binding, ssl, port, env, public_folder, logging,
|
||||||
always_rescue, error_handler, serve_static, server
|
always_rescue, serve_static, server
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@host_binding = "0.0.0.0"
|
@host_binding = "0.0.0.0"
|
||||||
|
@ -14,9 +16,11 @@ module Kemal
|
||||||
@public_folder = "./public"
|
@public_folder = "./public"
|
||||||
@logging = true
|
@logging = true
|
||||||
@logger = nil
|
@logger = nil
|
||||||
@always_rescue = true
|
|
||||||
@error_handler = nil
|
@error_handler = nil
|
||||||
@server = uninitialized HTTP::Server
|
@always_rescue = true
|
||||||
|
@run = false
|
||||||
|
@ssl = nil
|
||||||
|
@server = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def logger
|
def logger
|
||||||
|
@ -60,7 +64,7 @@ module Kemal
|
||||||
|
|
||||||
private def setup_error_handler
|
private def setup_error_handler
|
||||||
if @always_rescue
|
if @always_rescue
|
||||||
@error_handler ||= Kemal::CommonExceptionHandler::INSTANCE
|
@error_handler ||= Kemal::CommonExceptionHandler.new
|
||||||
HANDLERS.insert(1, @error_handler.not_nil!)
|
HANDLERS.insert(1, @error_handler.not_nil!)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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?
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ module Kemal::Middleware
|
||||||
|
|
||||||
# :nodoc: This shouldn't be called directly, it's not private because I need to call it for testing purpose since I can't call the macros in the spec.
|
# :nodoc: This shouldn't be called directly, it's not private because I need to call it for testing purpose since I can't call the macros in the spec.
|
||||||
# It adds the block for the corresponding verb/path/type combination to the tree.
|
# It adds the block for the corresponding verb/path/type combination to the tree.
|
||||||
def _add_route_filter(verb, path, type, &block : HTTP::Server::Context -> _)
|
def _add_route_filter(verb, path, type, &block : HTTP::Server::Context -> String)
|
||||||
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)
|
||||||
|
@ -33,12 +33,12 @@ module Kemal::Middleware
|
||||||
end
|
end
|
||||||
|
|
||||||
# This can be called directly but it's simpler to just use the macros, it will check if another filter is not already defined for this verb/path/type and proceed to call `add_route_filter`
|
# This can be called directly but it's simpler to just use the macros, it will check if another filter is not already defined for this verb/path/type and proceed to call `add_route_filter`
|
||||||
def before(verb, path = "*", &block : HTTP::Server::Context -> _)
|
def before(verb, path = "*", &block : HTTP::Server::Context -> String)
|
||||||
_add_route_filter verb, path, :before, &block
|
_add_route_filter verb, path, :before, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
# This can be called directly but it's simpler to just use the macros, it will check if another filter is not already defined for this verb/path/type and proceed to call `add_route_filter`
|
# This can be called directly but it's simpler to just use the macros, it will check if another filter is not already defined for this verb/path/type and proceed to call `add_route_filter`
|
||||||
def after(verb, path = "*", &block : HTTP::Server::Context -> _)
|
def after(verb, path = "*", &block : HTTP::Server::Context -> String)
|
||||||
_add_route_filter verb, path, :after, &block
|
_add_route_filter verb, path, :after, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -68,9 +68,9 @@ module Kemal::Middleware
|
||||||
end
|
end
|
||||||
|
|
||||||
class Block
|
class Block
|
||||||
property block
|
property block : (HTTP::Server::Context -> String)
|
||||||
|
|
||||||
def initialize(&@block : HTTP::Server::Context -> _)
|
def initialize(&@block : HTTP::Server::Context -> String)
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(context)
|
def call(context)
|
||||||
|
@ -86,7 +86,7 @@ end
|
||||||
ALL_METHODS = %w(get post put patch delete all)
|
ALL_METHODS = %w(get post put patch delete all)
|
||||||
{% for type in ["before", "after"] %}
|
{% for type in ["before", "after"] %}
|
||||||
{% for method in ALL_METHODS %}
|
{% for method in ALL_METHODS %}
|
||||||
def {{type.id}}_{{method.id}}(path = "*", &block : HTTP::Server::Context -> _)
|
def {{type.id}}_{{method.id}}(path = "*", &block : HTTP::Server::Context -> String)
|
||||||
Kemal::Middleware::Filter::INSTANCE.{{type.id}}({{method}}.upcase, path, &block)
|
Kemal::Middleware::Filter::INSTANCE.{{type.id}}({{method}}.upcase, path, &block)
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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 = {} 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) %}
|
||||||
|
|
|
@ -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!
|
||||||
|
|
|
@ -3,8 +3,10 @@
|
||||||
# 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
|
||||||
getter method
|
@handler : HTTP::Server::Context -> String
|
||||||
|
@method : String
|
||||||
|
|
||||||
def initialize(@method, @path, &@handler : HTTP::Server::Context -> _)
|
def initialize(@method, @path : String, &handler : HTTP::Server::Context -> _)
|
||||||
|
@handler = ->(context : HTTP::Server::Context) { handler.call(context).to_s }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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)
|
||||||
|
@ -33,7 +33,7 @@ class Kemal::RouteHandler < HTTP::Handler
|
||||||
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
|
||||||
context.response.print(route.handler.call(context).to_s)
|
context.response.print(route.handler.call(context))
|
||||||
context
|
context
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue