Created
August 24, 2014 18:26
-
-
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).
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 "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 |
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 "./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