mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
All specs passing except macros
This commit is contained in:
parent
c6e9e7a827
commit
d1f95c0f39
17 changed files with 243 additions and 247 deletions
|
@ -1,31 +1,11 @@
|
|||
# Context is the environment which holds request/response specific
|
||||
# information such as params, content_type e.g
|
||||
class Kemal::Context
|
||||
getter request
|
||||
getter response
|
||||
getter route
|
||||
getter content_type
|
||||
class HTTP::Server
|
||||
class Context
|
||||
getter params
|
||||
|
||||
def initialize(@request, @route)
|
||||
@response = Kemal::Response.new
|
||||
def params
|
||||
Kemal::ParamParser.new(@route, @request).parse
|
||||
end
|
||||
end
|
||||
|
||||
def response_headers
|
||||
@response.headers
|
||||
end
|
||||
|
||||
def add_header(name, value)
|
||||
@response.headers.add name, value
|
||||
end
|
||||
|
||||
def content_type
|
||||
@response.content_type
|
||||
end
|
||||
|
||||
def params
|
||||
Kemal::ParamParser.new(@route, @request).parse
|
||||
end
|
||||
|
||||
delegate headers, @request
|
||||
delegate status_code, :"status_code=", :"content_type=", @response
|
||||
end
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
HTTP_METHODS = %w(get post put patch delete)
|
||||
|
||||
{% for method in HTTP_METHODS %}
|
||||
def {{method.id}}(path, &block : Kemal::Context -> _)
|
||||
def {{method.id}}(path, &block : HTTP::Server::Context -> _)
|
||||
Kemal::Handler::INSTANCE.add_route({{method}}.upcase, path, &block)
|
||||
end
|
||||
{% end %}
|
||||
|
||||
def ws(path, &block : HTTP::WebSocketHandler::WebSocketSession -> _)
|
||||
def ws(path, &block : HTTP::WebSocketHandler::WebSocket -> _)
|
||||
Kemal::WebSocketHandler.new path, &block
|
||||
end
|
||||
|
|
|
@ -11,37 +11,39 @@ class Kemal::Handler < HTTP::Handler
|
|||
@tree = Beryl::Routing::Tree.new
|
||||
end
|
||||
|
||||
def call(request)
|
||||
response = process_request(request)
|
||||
response || call_next(request)
|
||||
def call(context)
|
||||
response = process_request(context)
|
||||
response || call_next(context)
|
||||
end
|
||||
|
||||
def add_route(method, path, &handler : Kemal::Context -> _)
|
||||
def add_route(method, path, &handler : HTTP::Server::Context -> _)
|
||||
add_to_radix_tree method, path, Route.new(method, path, &handler)
|
||||
|
||||
# Registering HEAD route for defined GET routes.
|
||||
add_to_radix_tree("HEAD", path, Route.new("HEAD", path, &handler)) if method == "GET"
|
||||
end
|
||||
|
||||
def process_request(request)
|
||||
url = request.path.not_nil!
|
||||
Kemal::Route.check_for_method_override!(request)
|
||||
lookup = @tree.find radix_path(request.override_method, request.path)
|
||||
def process_request(context)
|
||||
url = context.request.path.not_nil!
|
||||
Kemal::Route.check_for_method_override!(context.request)
|
||||
lookup = @tree.find radix_path(context.request.override_method, context.request.path)
|
||||
if lookup.found?
|
||||
route = lookup.payload as Route
|
||||
if route.match?(request)
|
||||
context = Context.new(request, route)
|
||||
if route.match?(context.request)
|
||||
begin
|
||||
body = route.handler.call(context).to_s
|
||||
return HTTP::Response.new(context.status_code, body, context.response_headers)
|
||||
# context.response.status_code = 200
|
||||
# context.response.content_type = "text/html"
|
||||
context.response.print body
|
||||
return context
|
||||
rescue ex
|
||||
Kemal::Logger::INSTANCE.write "Exception: #{ex.to_s}\n"
|
||||
return render_500(ex.to_s)
|
||||
return render_500(context, ex.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
# Render 404 unless a route matches
|
||||
return render_404
|
||||
return render_404(context)
|
||||
end
|
||||
|
||||
private def radix_path(method, path)
|
||||
|
|
|
@ -15,26 +15,26 @@ class Kemal::Logger < HTTP::Handler
|
|||
end
|
||||
end
|
||||
|
||||
def call(request)
|
||||
def call(context)
|
||||
time = Time.now
|
||||
response = call_next(request)
|
||||
call_next(context)
|
||||
elapsed = Time.now - time
|
||||
elapsed_text = elapsed_text(elapsed)
|
||||
|
||||
if @env == "production"
|
||||
status_code = " #{response.status_code} "
|
||||
method = request.method
|
||||
status_code = " #{context.response.status_code} "
|
||||
method = context.request.method
|
||||
else
|
||||
statusColor = color_for_status(response.status_code)
|
||||
methodColor = color_for_method(request.method)
|
||||
statusColor = color_for_status(context.response.status_code)
|
||||
methodColor = color_for_method(context.request.method)
|
||||
|
||||
status_code = " #{response.status_code} ".colorize.back(statusColor).fore(:white)
|
||||
method = request.method.colorize(methodColor)
|
||||
status_code = " #{context.response.status_code} ".colorize.back(statusColor).fore(:white)
|
||||
method = context.request.method.colorize(methodColor)
|
||||
end
|
||||
|
||||
output_message = "#{time} |#{status_code}| #{method} #{request.resource} - #{elapsed_text}\n"
|
||||
output_message = "#{time} |#{status_code}| #{method} #{context.request.resource} - #{elapsed_text}\n"
|
||||
write output_message
|
||||
response
|
||||
context
|
||||
end
|
||||
|
||||
private def elapsed_text(elapsed)
|
||||
|
|
|
@ -16,17 +16,18 @@ module Kemal::Middleware
|
|||
def initialize(@username, @password)
|
||||
end
|
||||
|
||||
def call(request)
|
||||
if request.headers[AUTH]?
|
||||
if value = request.headers[AUTH]
|
||||
def call(context)
|
||||
if context.request.headers[AUTH]?
|
||||
if value = context.request.headers[AUTH]
|
||||
if value.size > 0 && value.starts_with?(BASIC)
|
||||
return call_next(request) if authorized?(value)
|
||||
return call_next(context) if authorized?(value)
|
||||
end
|
||||
end
|
||||
end
|
||||
headers = HTTP::Headers.new
|
||||
headers["WWW-Authenticate"] = HEADER_LOGIN_REQUIRED
|
||||
HTTP::Response.new(401, AUTH_MESSAGE, headers, nil, "HTTP/1.1", nil)
|
||||
context.response.status_code = 401
|
||||
context.response.headers["WWW-Authenticate"] = HEADER_LOGIN_REQUIRED
|
||||
context.response.print AUTH_MESSAGE
|
||||
end
|
||||
|
||||
def authorized?(value)
|
||||
|
|
|
@ -5,7 +5,7 @@ class Kemal::Route
|
|||
getter handler
|
||||
getter method
|
||||
|
||||
def initialize(@method, @path, &@handler : Kemal::Context -> _)
|
||||
def initialize(@method, @path, &@handler : HTTP::Server::Context -> _)
|
||||
@compiled_regex = pattern_to_regex(@path)
|
||||
end
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# Template for 403 Forbidden
|
||||
def render_403
|
||||
def render_403(context)
|
||||
template = <<-HTML
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
@ -17,11 +17,12 @@ def render_403
|
|||
</body>
|
||||
</html>
|
||||
HTML
|
||||
HTTP::Response.new(403, template)
|
||||
context.response.status_code = 403
|
||||
context.response.puts template
|
||||
end
|
||||
|
||||
# Template for 404 Not Found
|
||||
def render_404
|
||||
def render_404(context)
|
||||
template = <<-HTML
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
@ -38,11 +39,12 @@ def render_404
|
|||
</body>
|
||||
</html>
|
||||
HTML
|
||||
HTTP::Response.new(404, template)
|
||||
context.response.status_code = 404
|
||||
context.response.puts template
|
||||
end
|
||||
|
||||
# Template for 500 Internal Server Error
|
||||
def render_500(ex)
|
||||
def render_500(context, ex)
|
||||
template = <<-HTML
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
@ -59,5 +61,6 @@ def render_500(ex)
|
|||
</body>
|
||||
</html>
|
||||
HTML
|
||||
HTTP::Response.error("text/html", template)
|
||||
context.response.status_code = 500
|
||||
context.response.puts template
|
||||
end
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
# For each WebSocket route a new handler is created and registered to global handlers.
|
||||
|
||||
class Kemal::WebSocketHandler < HTTP::WebSocketHandler
|
||||
def initialize(@path, &@proc : WebSocketSession ->)
|
||||
def initialize(@path, &@proc : HTTP::WebSocket ->)
|
||||
Kemal.config.add_ws_handler self
|
||||
end
|
||||
|
||||
def call(request)
|
||||
return call_next(request) unless request.path.not_nil! == @path
|
||||
def call(context)
|
||||
return call_next(context) unless context.request.path.not_nil! == @path
|
||||
super
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue