I have a simple Rack app hosted on Heroku. config.ru:
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)
]
}
How can I add HTTP Basic Auth to this? Bonus points if it only works in the production environment.
Thanks
If you want to also protect images, stylesheets and javascripts behind basic auth, you need to put Rack::Auth::Basic first:
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)
]
}
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)
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With