mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Improve kemal.cr
This commit is contained in:
parent
fe10d210c3
commit
092f5a2e49
13 changed files with 13 additions and 24 deletions
|
@ -28,5 +28,4 @@ describe "Config" do
|
||||||
config.add_handler CustomTestHandler.new
|
config.add_handler CustomTestHandler.new
|
||||||
config.handlers.size.should eq(1)
|
config.handlers.size.should eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,7 +27,6 @@ describe "Macros" do
|
||||||
logging false
|
logging false
|
||||||
Kemal.config.logging.should eq false
|
Kemal.config.logging.should eq false
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sets a custom logger" do
|
it "sets a custom logger" do
|
||||||
config = Kemal::Config::INSTANCE
|
config = Kemal::Config::INSTANCE
|
||||||
logger CustomLogHandler.new("production")
|
logger CustomLogHandler.new("production")
|
||||||
|
|
|
@ -109,13 +109,13 @@ describe "Kemal::RouteHandler" do
|
||||||
end
|
end
|
||||||
|
|
||||||
# Removed until there is a way to test multiple middlewares
|
# Removed until there is a way to test multiple middlewares
|
||||||
#it "renders 404 on not found" do
|
# it "renders 404 on not found" do
|
||||||
# kemal = Kemal::RouteHandler.new
|
# kemal = Kemal::RouteHandler.new
|
||||||
# request = HTTP::Request.new("GET", "/?message=world")
|
# request = HTTP::Request.new("GET", "/?message=world")
|
||||||
# io_with_context = create_request_and_return_io(kemal, request)
|
# io_with_context = create_request_and_return_io(kemal, request)
|
||||||
# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
||||||
# client_response.status_code.should eq 404
|
# client_response.status_code.should eq 404
|
||||||
#end
|
# end
|
||||||
|
|
||||||
# it "renders 500 on exception" do
|
# it "renders 500 on exception" do
|
||||||
# kemal = Kemal::RouteHandler.new
|
# kemal = Kemal::RouteHandler.new
|
||||||
|
@ -190,13 +190,13 @@ describe "Kemal::RouteHandler" do
|
||||||
end
|
end
|
||||||
|
|
||||||
# Removed until there is a way to test multiple middlewares
|
# Removed until there is a way to test multiple middlewares
|
||||||
#it "can't process HTTP HEAD requests for undefined GET routes" do
|
# it "can't process HTTP HEAD requests for undefined GET routes" do
|
||||||
# kemal = Kemal::RouteHandler.new
|
# kemal = Kemal::RouteHandler.new
|
||||||
# request = HTTP::Request.new("HEAD", "/")
|
# request = HTTP::Request.new("HEAD", "/")
|
||||||
# io_with_context = create_request_and_return_io(kemal, request)
|
# io_with_context = create_request_and_return_io(kemal, request)
|
||||||
# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
||||||
# client_response.status_code.should eq(404)
|
# client_response.status_code.should eq(404)
|
||||||
#end
|
# end
|
||||||
|
|
||||||
it "redirects user to provided url" do
|
it "redirects user to provided url" do
|
||||||
kemal = Kemal::RouteHandler.new
|
kemal = Kemal::RouteHandler.new
|
||||||
|
|
|
@ -5,9 +5,8 @@ at_exit do
|
||||||
Kemal::CLI.new
|
Kemal::CLI.new
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
config.setup_logging
|
config.setup_logging
|
||||||
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
|
|
||||||
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)
|
|
||||||
config.setup_error_handler
|
config.setup_error_handler
|
||||||
|
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)
|
||||||
config.add_handler Kemal::RouteHandler::INSTANCE
|
config.add_handler Kemal::RouteHandler::INSTANCE
|
||||||
|
|
||||||
server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers)
|
server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers)
|
||||||
|
@ -31,6 +30,7 @@ at_exit do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
|
||||||
server.listen
|
server.listen
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
require "http"
|
require "http"
|
||||||
|
|
||||||
class Kemal::BaseLogHandler < HTTP::Handler
|
class Kemal::BaseLogHandler < HTTP::Handler
|
||||||
|
|
||||||
def initialize(@env)
|
def initialize(@env)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,5 +12,4 @@ class Kemal::CommonErrorHandler < HTTP::Handler
|
||||||
return render_500(context, ex.to_s)
|
return render_500(context, ex.to_s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
require "colorize"
|
require "colorize"
|
||||||
require "http"
|
require "http"
|
||||||
|
|
||||||
class Kemal::CommonLogHandler < Kemal::BaseLogHandler
|
class Kemal::CommonLogHandler < Kemal::BaseLogHandler
|
||||||
# INSTANCE = new
|
|
||||||
getter handler
|
getter handler
|
||||||
|
|
||||||
def initialize(@env)
|
def initialize(@env)
|
||||||
|
|
|
@ -55,7 +55,6 @@ module Kemal
|
||||||
HANDLERS << @error_handler.not_nil!
|
HANDLERS << @error_handler.not_nil!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.config
|
def self.config
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
module Kemal::Exceptions
|
module Kemal::Exceptions
|
||||||
|
|
||||||
class RouteNotFound < Exception
|
class RouteNotFound < Exception
|
||||||
def initialize(context)
|
def initialize(context)
|
||||||
super "Requested path: '#{context.request.override_method as String}:#{context.request.path}' was not found."
|
super "Requested path: '#{context.request.override_method as String}:#{context.request.path}' was not found."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -40,11 +40,10 @@ end
|
||||||
# Enables / Disables logging
|
# Enables / Disables logging
|
||||||
macro logging(status)
|
macro logging(status)
|
||||||
Kemal.config.logging = {{status}}
|
Kemal.config.logging = {{status}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
macro logger(logger)
|
||||||
|
|
||||||
macro logger(logger)
|
|
||||||
Kemal.config.logger = {{logger}}
|
Kemal.config.logger = {{logger}}
|
||||||
Kemal.config.add_handler {{logger}}
|
Kemal.config.add_handler {{logger}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -45,15 +45,14 @@ module Kemal::Middleware
|
||||||
super "An after-filter is already defined for path: '#{path}'."
|
super "An after-filter is already defined for path: '#{path}'."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Block
|
class Block
|
||||||
property block
|
property block
|
||||||
|
|
||||||
def initialize(&@block : HTTP::Server::Context -> _)
|
def initialize(&@block : HTTP::Server::Context -> _)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_filters
|
def add_filters
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
class Kemal::NullLogHandler < Kemal::BaseLogHandler
|
class Kemal::NullLogHandler < Kemal::BaseLogHandler
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,5 +41,4 @@ class Kemal::RouteHandler < HTTP::Handler
|
||||||
node = radix_path method, path
|
node = radix_path method, path
|
||||||
@tree.add node, route
|
@tree.add node, route
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue