Skip to content

Instantly share code, notes, and snippets.

@rennex
Created August 24, 2014 18:26
Show Gist options
  • Save rennex/70a528638d4a0f512e46 to your computer and use it in GitHub Desktop.
Save rennex/70a528638d4a0f512e46 to your computer and use it in GitHub Desktop.
Rack middleware that sends requests to different Sinatra apps based on some criteria (randomly in this example).
require "sinatra/base"
class Foo < Sinatra::Base
get "/" do
"foo!"
end
end
class Bar < Sinatra::Base
get "/" do
"bar!"
end
end
class Chooser
def initialize(app1, app2)
@app1 = app1
@app2 = app2
end
def call(env)
if rand(2) == 0
@app1.call(env)
else
@app2.call(env)
end
end
end
require "./chooser_middleware.rb"
run Chooser.new(Foo, Bar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment