Created
March 5, 2017 21:42
-
-
Save jwoertink/d01f3ba34f757f94a5417324386e58f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "kemal" | |
class RequestPathMiddleware < Kemal::Handler | |
def initialize(@number : String, @use_header : Bool = false) | |
end | |
def call(env) | |
if @use_header | |
process_request(env) | |
end | |
call_next(env) | |
end | |
private def process_request(env) | |
if env.request.headers["Accept"] && | |
env.request.headers["Accept"].match(/application\/vnd\.api\.(v\d+)\+json/) && | |
$1 == "v#{@number}" | |
puts "Matched on number: #{@number}" | |
env.request.path = "/route" | |
end | |
end | |
end | |
add_handler RequestPathMiddleware.new("1") | |
add_handler RequestPathMiddleware.new("2") | |
add_handler RequestPathMiddleware.new("3", true) | |
get "/" do |env| | |
"Home" | |
end | |
get "/route" do |env| | |
"Route matched" | |
end | |
Kemal.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment