mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
resolve merge conflict by trusting sdogruyol/master RE property listings
This commit is contained in:
commit
b73802df0e
25 changed files with 197 additions and 207 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
|
||||||
|
|
|
@ -1,11 +1,28 @@
|
||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
|
|
||||||
describe "Kemal::CommonExceptionHandler" do
|
describe "Kemal::CommonExceptionHandler" do
|
||||||
it "renders 404 on route not found" do
|
# it "renders 404 on route not found" do
|
||||||
common_exception_handler = Kemal::CommonExceptionHandler::INSTANCE
|
# get "/" do |env|
|
||||||
request = HTTP::Request.new("GET", "/?message=world")
|
# "Hello"
|
||||||
io_with_context = create_request_and_return_io(common_exception_handler, request)
|
# end
|
||||||
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
|
#
|
||||||
client_response.status_code.should eq 404
|
# request = HTTP::Request.new("GET", "/asd")
|
||||||
end
|
# client_response = call_request_on_app(request)
|
||||||
|
# client_response.status_code.should eq 404
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# it "renders custom error" do
|
||||||
|
# error 403 do
|
||||||
|
# "403 error"
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# get "/" do |env|
|
||||||
|
# env.response.status_code = 403
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# request = HTTP::Request.new("GET", "/")
|
||||||
|
# client_response = call_request_on_app(request)
|
||||||
|
# client_response.status_code.should eq 403
|
||||||
|
# client_response.body.should eq "403 error"
|
||||||
|
# end
|
||||||
end
|
end
|
||||||
|
|
|
@ -34,4 +34,37 @@ describe "Macros" do
|
||||||
config.logger.should be_a(CustomLogHandler)
|
config.logger.should be_a(CustomLogHandler)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#return_with" do
|
||||||
|
it "can break block with return_with macro" do
|
||||||
|
get "/non-breaking" do |env|
|
||||||
|
"hello"
|
||||||
|
"world"
|
||||||
|
end
|
||||||
|
request = HTTP::Request.new("GET", "/non-breaking")
|
||||||
|
client_response = call_request_on_app(request)
|
||||||
|
client_response.status_code.should eq(200)
|
||||||
|
client_response.body.should eq("world")
|
||||||
|
|
||||||
|
get "/breaking" do |env|
|
||||||
|
return_with env, 404, "hello"
|
||||||
|
"world"
|
||||||
|
end
|
||||||
|
request = HTTP::Request.new("GET", "/breaking")
|
||||||
|
client_response = call_request_on_app(request)
|
||||||
|
client_response.status_code.should eq(404)
|
||||||
|
client_response.body.should eq("hello")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can break block with return_with macro using default values" do
|
||||||
|
get "/" do |env|
|
||||||
|
return_with env
|
||||||
|
"world"
|
||||||
|
end
|
||||||
|
request = HTTP::Request.new("GET", "/")
|
||||||
|
client_response = call_request_on_app(request)
|
||||||
|
client_response.status_code.should eq(200)
|
||||||
|
client_response.body.should eq("")
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -186,5 +186,5 @@ describe "Kemal::Middleware::Filters" do
|
||||||
end
|
end
|
||||||
|
|
||||||
class FilterTest
|
class FilterTest
|
||||||
property modified
|
property modified : String?
|
||||||
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
|
||||||
|
|
20
src/kemal.cr
20
src/kemal.cr
|
@ -2,18 +2,25 @@ require "./kemal/*"
|
||||||
require "./kemal/middleware/*"
|
require "./kemal/middleware/*"
|
||||||
|
|
||||||
module Kemal
|
module Kemal
|
||||||
|
# The command to run a `Kemal` application.
|
||||||
def self.run
|
def self.run
|
||||||
Kemal::CLI.new
|
Kemal::CLI.new
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
config.setup
|
config.setup
|
||||||
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)
|
config.server = HTTP::Server.new(config.host_binding.not_nil!, config.port, config.handlers)
|
||||||
server.ssl = config.ssl
|
config.server.not_nil!.ssl = config.ssl
|
||||||
|
|
||||||
|
error 404 do |env|
|
||||||
|
render_404(env)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Test environment doesn't need to have signal trap, built-in images, and logging.
|
||||||
|
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"
|
||||||
server.close
|
config.server.not_nil!.close
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,10 +38,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"
|
||||||
server.listen
|
config.server.not_nil!.listen
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
at_exit do
|
|
||||||
Kemal.run if Kemal.config.run
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
require "http"
|
require "http"
|
||||||
|
|
||||||
|
# All loggers must inherit from `Kemal::BaseLogHandler`.
|
||||||
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)
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
require "option_parser"
|
require "option_parser"
|
||||||
|
|
||||||
module Kemal
|
module Kemal
|
||||||
|
# Handles all the initialization from the command line.
|
||||||
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
|
||||||
|
|
|
@ -1,18 +1,24 @@
|
||||||
class Kemal::CommonExceptionHandler < HTTP::Handler
|
module Kemal
|
||||||
|
class CommonExceptionHandler < HTTP::Handler
|
||||||
INSTANCE = new
|
INSTANCE = new
|
||||||
|
|
||||||
def call(context)
|
def call(context)
|
||||||
begin
|
begin
|
||||||
call_next context
|
call_next(context)
|
||||||
rescue ex : Kemal::Exceptions::RouteNotFound
|
rescue Kemal::Exceptions::RouteNotFound
|
||||||
|
return Kemal.config.error_handlers[404].call(context)
|
||||||
|
rescue Kemal::Exceptions::CustomException
|
||||||
|
status_code = context.response.status_code
|
||||||
|
if Kemal.config.error_handlers.has_key?(status_code)
|
||||||
|
context.response.print Kemal.config.error_handlers[status_code].call(context)
|
||||||
|
return context
|
||||||
|
end
|
||||||
|
rescue ex : Exception
|
||||||
context.response.content_type = "text/html"
|
context.response.content_type = "text/html"
|
||||||
Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace.colorize(:red)}\n")
|
Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace}\n")
|
||||||
return render_404(context)
|
|
||||||
rescue ex
|
|
||||||
context.response.content_type = "text/html"
|
|
||||||
Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace.colorize(:red)}\n")
|
|
||||||
verbosity = Kemal.config.env == "production" ? false : true
|
verbosity = Kemal.config.env == "production" ? false : true
|
||||||
return render_500(context, ex.inspect_with_backtrace, verbosity)
|
return render_500(context, ex.inspect_with_backtrace, verbosity)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,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)
|
||||||
|
@ -19,20 +19,7 @@ class Kemal::CommonLogHandler < Kemal::BaseLogHandler
|
||||||
call_next(context)
|
call_next(context)
|
||||||
elapsed = Time.now - time
|
elapsed = Time.now - time
|
||||||
elapsed_text = elapsed_text(elapsed)
|
elapsed_text = elapsed_text(elapsed)
|
||||||
|
write "#{time} #{context.response.status_code} #{context.request.method} #{context.request.resource} - #{elapsed_text}\n"
|
||||||
if @env == "production"
|
|
||||||
status_code = " #{context.response.status_code} "
|
|
||||||
method = context.request.method
|
|
||||||
else
|
|
||||||
statusColor = color_for_status(context.response.status_code)
|
|
||||||
methodColor = color_for_method(context.request.method)
|
|
||||||
|
|
||||||
status_code = " #{context.response.status_code} ".colorize.back(statusColor).fore(:white)
|
|
||||||
method = context.request.method.colorize(methodColor)
|
|
||||||
end
|
|
||||||
|
|
||||||
output_message = "#{time} |#{status_code}| #{method} #{context.request.resource} - #{elapsed_text}\n"
|
|
||||||
write output_message
|
|
||||||
context
|
context
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -56,37 +43,4 @@ class Kemal::CommonLogHandler < Kemal::BaseLogHandler
|
||||||
@handler.print message
|
@handler.print message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def color_for_status(code)
|
|
||||||
if code >= 200 && code < 300
|
|
||||||
return :green
|
|
||||||
elsif code >= 300 && code < 400
|
|
||||||
return :magenta
|
|
||||||
elsif code >= 400 && code < 500
|
|
||||||
return :yellow
|
|
||||||
else
|
|
||||||
return :light_blue
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private def color_for_method(method)
|
|
||||||
case method
|
|
||||||
when "GET"
|
|
||||||
return :blue
|
|
||||||
when "POST"
|
|
||||||
return :cyan
|
|
||||||
when "PUT"
|
|
||||||
return :yellow
|
|
||||||
when "DELETE"
|
|
||||||
return :red
|
|
||||||
when "PATCH"
|
|
||||||
return :green
|
|
||||||
when "HEAD"
|
|
||||||
return :magenta
|
|
||||||
when "OPTIONS"
|
|
||||||
return :light_blue
|
|
||||||
else
|
|
||||||
return :white
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,8 +2,12 @@ module Kemal
|
||||||
class Config
|
class Config
|
||||||
INSTANCE = Config.new
|
INSTANCE = Config.new
|
||||||
HANDLERS = [] of HTTP::Handler
|
HANDLERS = [] of HTTP::Handler
|
||||||
|
ERROR_HANDLERS = {} of Int32 => HTTP::Server::Context -> String
|
||||||
|
@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, run, extra_options
|
always_rescue, serve_static, server, extra_options
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@host_binding = "0.0.0.0"
|
@host_binding = "0.0.0.0"
|
||||||
|
@ -13,8 +17,8 @@ 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
|
||||||
|
@always_rescue = true
|
||||||
@run = false
|
@run = false
|
||||||
@extra_options = nil
|
@extra_options = nil
|
||||||
end
|
end
|
||||||
|
@ -43,14 +47,22 @@ module Kemal
|
||||||
HANDLERS << handler
|
HANDLERS << handler
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def error_handlers
|
||||||
setup_logging
|
ERROR_HANDLERS
|
||||||
setup_error_handler
|
|
||||||
setup_public_folder
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup_logging
|
def add_error_handler(status_code, &handler : HTTP::Server::Context -> _)
|
||||||
@logger = if @logging
|
ERROR_HANDLERS[status_code] = ->(context : HTTP::Server::Context) { handler.call(context).to_s }
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
|
setup_log_handler
|
||||||
|
setup_error_handler
|
||||||
|
setup_static_file_handler
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup_log_handler
|
||||||
|
@logger ||= if @logging
|
||||||
Kemal::CommonLogHandler.new(@env)
|
Kemal::CommonLogHandler.new(@env)
|
||||||
else
|
else
|
||||||
Kemal::NullLogHandler.new(@env)
|
Kemal::NullLogHandler.new(@env)
|
||||||
|
@ -60,12 +72,12 @@ 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
|
||||||
|
|
||||||
private def setup_public_folder
|
private def setup_static_file_handler
|
||||||
HANDLERS.insert(2, Kemal::StaticFileHandler.new(@public_folder)) if @serve_static
|
HANDLERS.insert(2, Kemal::StaticFileHandler.new(@public_folder)) if @serve_static
|
||||||
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?
|
||||||
|
|
|
@ -6,6 +6,10 @@ HTTP_METHODS = %w(get post put patch delete options)
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
def ws(path, &block : HTTP::WebSocket -> _)
|
def ws(path, &block : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
||||||
Kemal::WebSocketHandler.new path, &block
|
Kemal::WebSocketHandler.new path, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def error(status_code, &block : HTTP::Server::Context -> _)
|
||||||
|
Kemal.config.add_error_handler status_code, &block
|
||||||
|
end
|
||||||
|
|
|
@ -4,4 +4,11 @@ module Kemal::Exceptions
|
||||||
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
|
||||||
|
|
||||||
|
class CustomException < Exception
|
||||||
|
|
||||||
|
def initialize(context)
|
||||||
|
super "Rendered error with #{context.response.status_code}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,6 @@ require "kilt"
|
||||||
# get '/' do
|
# get '/' do
|
||||||
# render 'hello.ecr'
|
# render 'hello.ecr'
|
||||||
# end
|
# end
|
||||||
|
|
||||||
macro render(filename, layout)
|
macro render(filename, layout)
|
||||||
content = render {{filename}}
|
content = render {{filename}}
|
||||||
render {{layout}}
|
render {{layout}}
|
||||||
|
@ -15,6 +14,13 @@ macro render(filename, *args)
|
||||||
Kilt.render({{filename}}, {{*args}})
|
Kilt.render({{filename}}, {{*args}})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
macro return_with(env, status_code = 200, response = "")
|
||||||
|
{{env}}.response.status_code = {{status_code}}
|
||||||
|
{{env}}.response.print {{response}}
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
# Adds given HTTP::Handler+ to handlers.
|
||||||
def add_handler(handler)
|
def add_handler(handler)
|
||||||
Kemal.config.add_handler handler
|
Kemal.config.add_handler handler
|
||||||
end
|
end
|
||||||
|
@ -25,6 +31,8 @@ def basic_auth(username, password)
|
||||||
add_handler auth_handler
|
add_handler auth_handler
|
||||||
end
|
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)
|
||||||
Kemal.config.public_folder = path
|
Kemal.config.public_folder = path
|
||||||
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
|
||||||
|
|
||||||
|
@ -15,6 +15,9 @@ module Kemal::Middleware
|
||||||
return call_next(context) unless context.route_defined?
|
return call_next(context) unless context.route_defined?
|
||||||
call_block_for_path_type("ALL", context.request.path, :before, context)
|
call_block_for_path_type("ALL", context.request.path, :before, context)
|
||||||
call_block_for_path_type(context.request.override_method, context.request.path, :before, context)
|
call_block_for_path_type(context.request.override_method, context.request.path, :before, context)
|
||||||
|
if Kemal.config.error_handlers.has_key?(context.response.status_code)
|
||||||
|
raise Kemal::Exceptions::CustomException.new(context)
|
||||||
|
end
|
||||||
call_next(context)
|
call_next(context)
|
||||||
call_block_for_path_type(context.request.override_method, context.request.path, :after, context)
|
call_block_for_path_type(context.request.override_method, context.request.path, :after, context)
|
||||||
call_block_for_path_type("ALL", context.request.path, :after, context)
|
call_block_for_path_type("ALL", context.request.path, :after, context)
|
||||||
|
@ -68,9 +71,10 @@ 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 -> _)
|
||||||
|
@block = ->(context : HTTP::Server::Context) { block.call(context).to_s}
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(context)
|
def call(context)
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
|
# This is here to represend the logger corresponding to Null Object Pattern.
|
||||||
class Kemal::NullLogHandler < Kemal::BaseLogHandler
|
class Kemal::NullLogHandler < Kemal::BaseLogHandler
|
||||||
end
|
end
|
||||||
|
|
|
@ -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,11 @@ 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)
|
content = route.handler.call(context)
|
||||||
|
if Kemal.config.error_handlers.has_key?(context.response.status_code)
|
||||||
|
raise Kemal::Exceptions::CustomException.new(context)
|
||||||
|
end
|
||||||
|
context.response.print(content)
|
||||||
context
|
context
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,3 @@
|
||||||
# Template for 403 Forbidden
|
|
||||||
def render_403(context)
|
|
||||||
template = <<-HTML
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<style type="text/css">
|
|
||||||
body { text-align:center;font-family:helvetica,arial;font-size:22px;
|
|
||||||
color:#888;margin:20px}
|
|
||||||
#c {margin:0 auto;width:500px;text-align:left}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h2>Forbidden</h2>
|
|
||||||
<h3>Kemal doesn't allow you to see this page.</h3>
|
|
||||||
<img src="/__kemal__/404.png">
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
HTML
|
|
||||||
context.response.content_type = "text/html"
|
|
||||||
context.response.status_code = 403
|
|
||||||
context.response.print template
|
|
||||||
context
|
|
||||||
end
|
|
||||||
|
|
||||||
# Template for 404 Not Found
|
# Template for 404 Not Found
|
||||||
def render_404(context)
|
def render_404(context)
|
||||||
template = <<-HTML
|
template = <<-HTML
|
||||||
|
@ -79,53 +54,3 @@ def render_500(context, backtrace, verbosity)
|
||||||
context.response.print template
|
context.response.print template
|
||||||
context
|
context
|
||||||
end
|
end
|
||||||
|
|
||||||
# Template for 415 Unsupported media type
|
|
||||||
def render_415(context, message)
|
|
||||||
template = <<-HTML
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<style type="text/css">
|
|
||||||
body { text-align:center;font-family:helvetica,arial;font-size:22px;
|
|
||||||
color:#888;margin:20px}
|
|
||||||
#c {margin:0 auto;width:500px;text-align:left}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h2>Unsupported media type</h2>
|
|
||||||
<h3>#{message}</h3>
|
|
||||||
<img src="/__kemal__/404.png">
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
HTML
|
|
||||||
context.response.content_type = "text/html"
|
|
||||||
context.response.status_code = 415
|
|
||||||
context.response.print template
|
|
||||||
context
|
|
||||||
end
|
|
||||||
|
|
||||||
# Template for 400 Bad request
|
|
||||||
def render_400(context, message)
|
|
||||||
template = <<-HTML
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<style type="text/css">
|
|
||||||
body { text-align:center;font-family:helvetica,arial;font-size:22px;
|
|
||||||
color:#888;margin:20px}
|
|
||||||
#c {margin:0 auto;width:500px;text-align:left}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h2>Bad request</h2>
|
|
||||||
<h3>#{message}</h3>
|
|
||||||
<img src="/__kemal__/404.png">
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
HTML
|
|
||||||
context.response.content_type = "text/html"
|
|
||||||
context.response.status_code = 400
|
|
||||||
context.response.print template
|
|
||||||
context
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# Kemal::WebSocketHandler is used for each define WebSocket route.
|
# Kemal::WebSocketHandler is used for building a 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 ->)
|
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