2011-08-12 18 views
8

私はHerokuでホストされている簡単なRackアプリケーションを持っています。 config.ru:HTTPの基本認証:Herokuの静的アプリケーション

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基本認証を追加するにはどうすればよいですか?プロダクション環境でのみ動作するボーナスポイント。

あなたはまた、基本的な認証の後ろに画像、スタイルシートとJavaScriptを保護したい場合はおかげ

答えて

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

は、あなたがラックを配置する必要があります:: Authの::基本最初:

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) 
    ] 
}