Skip to content

Instantly share code, notes, and snippets.

@SaitoWu
Created June 12, 2012 11:38
Show Gist options
  • Save SaitoWu/2917025 to your computer and use it in GitHub Desktop.
Save SaitoWu/2917025 to your computer and use it in GitHub Desktop.
rack builder
require 'rack'
require 'rack/auth/basic'
class HelloWorld
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello World!"]
end
end
class WorldHello
def call(env)
[200, {"Content-Type" => "text/html"}, "World Hello!"]
end
end
class Guard
def initialize(app)
@app = app
end
def call(env)
if env['PATH_INFO'] =~ Regexp.new('^/api')
GRACK.call(env)
else
@app.call(env)
end
end
end
GRACK = Rack::Builder.new do
use Rack::Auth::Basic do |username, password|
true
end
run WorldHello.new
end
BUILDER = Rack::Builder.new do
use Guard
run HelloWorld.new
end
# try http://localhost:3000 => response "HelloWorld"
# try http://localhost:3000/api => challenge and response "WorldHello"
run BUILDER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment