2016-10-03 22 views
0

我怎麼能添加此如何添加驗證::基本以簡單的Rack應用

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

這個

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 

我有這個簡單的機架應用程序,我需要添加驗證::基本。

謝謝

回答

1

您需要使用Rack :: Builder來組合一堆機架應用程序。

例子:

# 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 

,並啓動它:

$ rackup app.ru 
相關問題