Created
June 12, 2012 11:38
-
-
Save SaitoWu/2917025 to your computer and use it in GitHub Desktop.
rack builder
This file contains 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 '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