Skip to content

Instantly share code, notes, and snippets.

@jwoertink
Created March 5, 2017 21:42
Show Gist options
  • Save jwoertink/d01f3ba34f757f94a5417324386e58f7 to your computer and use it in GitHub Desktop.
Save jwoertink/d01f3ba34f757f94a5417324386e58f7 to your computer and use it in GitHub Desktop.
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