2011-08-12 10 views
8

我有一個簡單的Rack應用託管在Heroku上。 config.ru:Heroku上Rack :: Static應用的HTTP基本認證

use Rack::Static, 
    :urls => ["/stylesheets", "/images", "/javascripts"], 
    :root => "public" 

run lambda { |env| 
    [ 
    200, 
    { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=86400' 
    }, 
    File.open('public/index.html', File::RDONLY) 
    ] 
} 

如何添加HTTP基本身份驗證到此?如果它僅適用於生產環境,則爲獎勵積分。

感謝

回答

14
use Rack::Static, 
    :urls => ["/stylesheets", "/images", "/javascripts"], 
    :root => "public" 

#SOLUTION: 
use Rack::Auth::Basic, "Restricted Area" do |username, password| 
    [username, password] == ['admin', 'admin'] 
end 

run lambda { |env| 
    [ 
    200, 
    { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=86400' 
    }, 
    File.open('public/index.html', File::RDONLY) 
    ] 
} 
5

如果你也想保護的圖像,樣式表和後面基本身份驗證的JavaScript,你需要把機架::驗證::基本第一:

use Rack::Auth::Basic, "Restricted Area" do |username, password| 
    [username, password] == ['admin', 'admin'] 
end 

use Rack::Static, 
    :urls => ["/stylesheets", "/images", "/javascripts"], 
    :root => "public" 

run lambda { |env| 
    [ 
    200, 
    { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=86400' 
    }, 
    File.open('public/index.html', File::RDONLY) 
    ] 
}