mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Actually there's no middleware anymore
This commit is contained in:
parent
1afb4cbfa8
commit
eb64b52f84
7 changed files with 17 additions and 19 deletions
|
@ -52,7 +52,7 @@ describe "Context" do
|
||||||
io = IO::Memory.new
|
io = IO::Memory.new
|
||||||
response = HTTP::Server::Response.new(io)
|
response = HTTP::Server::Response.new(io)
|
||||||
context = HTTP::Server::Context.new(request, response)
|
context = HTTP::Server::Context.new(request, response)
|
||||||
Kemal::Middleware::Filter::INSTANCE.call(context)
|
Kemal::FilterHandler::INSTANCE.call(context)
|
||||||
Kemal::RouteHandler::INSTANCE.call(context)
|
Kemal::RouteHandler::INSTANCE.call(context)
|
||||||
context.store["key"].should eq "value"
|
context.store["key"].should eq "value"
|
||||||
context.store["before_get"].should eq "Kemal"
|
context.store["before_get"].should eq "Kemal"
|
||||||
|
|
|
@ -49,7 +49,7 @@ end
|
||||||
|
|
||||||
describe "Handler" do
|
describe "Handler" do
|
||||||
it "adds custom handler before before_*" do
|
it "adds custom handler before before_*" do
|
||||||
filter_middleware = Kemal::Middleware::Filter.new
|
filter_middleware = Kemal::FilterHandler.new
|
||||||
filter_middleware._add_route_filter("GET", "/", :before) do |env|
|
filter_middleware._add_route_filter("GET", "/", :before) do |env|
|
||||||
env.response << " is"
|
env.response << " is"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
describe "Kemal::Middleware::Filters" do
|
describe "Kemal::FilterHandler" do
|
||||||
it "executes code before home request" do
|
it "executes code before home request" do
|
||||||
test_filter = FilterTest.new
|
test_filter = FilterTest.new
|
||||||
test_filter.modified = "false"
|
test_filter.modified = "false"
|
||||||
|
|
||||||
filter_middleware = Kemal::Middleware::Filter.new
|
filter_middleware = Kemal::FilterHandler.new
|
||||||
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = "true" }
|
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = "true" }
|
||||||
|
|
||||||
kemal = Kemal::RouteHandler::INSTANCE
|
kemal = Kemal::RouteHandler::INSTANCE
|
||||||
|
@ -23,7 +23,7 @@ describe "Kemal::Middleware::Filters" do
|
||||||
test_filter = FilterTest.new
|
test_filter = FilterTest.new
|
||||||
test_filter.modified = "false"
|
test_filter.modified = "false"
|
||||||
|
|
||||||
filter_middleware = Kemal::Middleware::Filter.new
|
filter_middleware = Kemal::FilterHandler.new
|
||||||
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
||||||
|
|
||||||
kemal = Kemal::RouteHandler::INSTANCE
|
kemal = Kemal::RouteHandler::INSTANCE
|
||||||
|
@ -49,7 +49,7 @@ describe "Kemal::Middleware::Filters" do
|
||||||
test_filter = FilterTest.new
|
test_filter = FilterTest.new
|
||||||
test_filter.modified = "false"
|
test_filter.modified = "false"
|
||||||
|
|
||||||
filter_middleware = Kemal::Middleware::Filter.new
|
filter_middleware = Kemal::FilterHandler.new
|
||||||
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
||||||
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
||||||
filter_middleware._add_route_filter("POST", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("POST", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
||||||
|
@ -77,7 +77,7 @@ describe "Kemal::Middleware::Filters" do
|
||||||
test_filter = FilterTest.new
|
test_filter = FilterTest.new
|
||||||
test_filter.modified = "false"
|
test_filter.modified = "false"
|
||||||
|
|
||||||
filter_middleware = Kemal::Middleware::Filter.new
|
filter_middleware = Kemal::FilterHandler.new
|
||||||
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = "true" }
|
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = "true" }
|
||||||
|
|
||||||
kemal = Kemal::RouteHandler::INSTANCE
|
kemal = Kemal::RouteHandler::INSTANCE
|
||||||
|
@ -95,7 +95,7 @@ describe "Kemal::Middleware::Filters" do
|
||||||
test_filter = FilterTest.new
|
test_filter = FilterTest.new
|
||||||
test_filter.modified = "false"
|
test_filter.modified = "false"
|
||||||
|
|
||||||
filter_middleware = Kemal::Middleware::Filter.new
|
filter_middleware = Kemal::FilterHandler.new
|
||||||
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
||||||
|
|
||||||
kemal = Kemal::RouteHandler::INSTANCE
|
kemal = Kemal::RouteHandler::INSTANCE
|
||||||
|
@ -121,7 +121,7 @@ describe "Kemal::Middleware::Filters" do
|
||||||
test_filter = FilterTest.new
|
test_filter = FilterTest.new
|
||||||
test_filter.modified = "false"
|
test_filter.modified = "false"
|
||||||
|
|
||||||
filter_middleware = Kemal::Middleware::Filter.new
|
filter_middleware = Kemal::FilterHandler.new
|
||||||
filter_middleware._add_route_filter("ALL", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("ALL", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
||||||
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
||||||
filter_middleware._add_route_filter("POST", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("POST", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
||||||
|
@ -152,7 +152,7 @@ describe "Kemal::Middleware::Filters" do
|
||||||
test_filter_third = FilterTest.new
|
test_filter_third = FilterTest.new
|
||||||
test_filter_third.modified = "false"
|
test_filter_third.modified = "false"
|
||||||
|
|
||||||
filter_middleware = Kemal::Middleware::Filter.new
|
filter_middleware = Kemal::FilterHandler.new
|
||||||
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
|
||||||
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_second.modified = test_filter_second.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_second.modified = test_filter_second.modified == "true" ? "false" : "true" }
|
||||||
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_third.modified = test_filter_third.modified == "true" ? "false" : "true" }
|
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_third.modified = test_filter_third.modified == "true" ? "false" : "true" }
|
||||||
|
|
|
@ -2,7 +2,6 @@ require "http"
|
||||||
require "multipart"
|
require "multipart"
|
||||||
require "./kemal/*"
|
require "./kemal/*"
|
||||||
require "./kemal/helpers/*"
|
require "./kemal/helpers/*"
|
||||||
require "./kemal/middleware/*"
|
|
||||||
|
|
||||||
module Kemal
|
module Kemal
|
||||||
# The command to run a `Kemal` application.
|
# The command to run a `Kemal` application.
|
||||||
|
|
|
@ -49,7 +49,7 @@ module Kemal
|
||||||
puts "SSL Certificate Not Found"
|
puts "SSL Certificate Not Found"
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
ssl = Kemal::Middleware::SSL.new
|
ssl = Kemal::SSL.new
|
||||||
ssl.set_key_file @key_file.not_nil!
|
ssl.set_key_file @key_file.not_nil!
|
||||||
ssl.set_cert_file @cert_file.not_nil!
|
ssl.set_cert_file @cert_file.not_nil!
|
||||||
Kemal.config.ssl = ssl.context
|
Kemal.config.ssl = ssl.context
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
module Kemal::Middleware
|
module Kemal
|
||||||
# Kemal::Filter handle all code that should be evaluated before and after
|
# Kemal::FilterHandler handle all code that should be evaluated before and after
|
||||||
# every request
|
# every request
|
||||||
class Filter < HTTP::Handler
|
class FilterHandler < HTTP::Handler
|
||||||
INSTANCE = new
|
INSTANCE = new
|
||||||
|
|
||||||
# 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(Array(Kemal::Middleware::FilterBlock)).new
|
@tree = Radix::Tree(Array(Kemal::FilterBlock)).new
|
||||||
Kemal.config.add_filter_handler(self)
|
Kemal.config.add_filter_handler(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ 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 -> _)
|
||||||
Kemal::Middleware::Filter::INSTANCE.{{type.id}}({{method}}.upcase, path, &block)
|
Kemal::FilterHandler::INSTANCE.{{type.id}}({{method}}.upcase, path, &block)
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
{% end %}
|
{% end %}
|
|
@ -1,5 +1,4 @@
|
||||||
module Kemal::Middleware
|
module Kemal
|
||||||
# This middleware adds SSL / TLS support.
|
|
||||||
class SSL
|
class SSL
|
||||||
getter context
|
getter context
|
||||||
|
|
Loading…
Reference in a new issue