2016-10-03 3 views
0

wie kann ich das, wie Auth hinzufügen :: Basic einfache Rack-Anwendung

use Rack::Auth::Basic do |username, password| 
    username == 'pippo' && password == 'pluto' 
end 

dieser

class HelloWorld 
    def call(env) 
    req = Rack::Request.new(env) 
    case req.path_info 
    when /badges/ 
     [200, {"Content-Type" => "text/html"}, ['This is great !!!!']] 
    when /goodbye/ 
     [500, {"Content-Type" => "text/html"}, ["Goodbye Cruel World!"]] 
    else 
     [404, {"Content-Type" => "text/html"}, ["I'm Lost!"]] 
    end 
    end 
end 


run HelloWorld.new 

ich diese einfache Rack-Anwendung haben hinzuzufügen, und ich brauche Auth hinzufügen :: Grund .

Dank

Antwort

1

Sie benötigen Rack-:: Builder verwenden, um einen Stapel von Rack-Anwendungen zu schreiben.

Beispiel:

# app.ru 
require 'rack' 

class HelloWorld 
    def call(env) 
    req = Rack::Request.new(env) 
    case req.path_info 
    when /badges/ 
     [200, {"Content-Type" => "text/html"}, ['This is great !!!!']] 
    when /goodbye/ 
     [500, {"Content-Type" => "text/html"}, ["Goodbye Cruel World!"]] 
    else 
     [404, {"Content-Type" => "text/html"}, ["I'm Lost!"]] 
    end 
    end 
end 

app = Rack::Builder.new do 
    use Rack::Auth::Basic do |username, password| 
    username == 'pippo' && password == 'pluto' 
    end 

    map '/' do 
    run HelloWorld.new 
    end 
end 

run app 

Und es zu starten:

$ rackup app.ru