-
-
Save Joseworks/e37b8cef884105da2933c78d1c45fccd to your computer and use it in GitHub Desktop.
Adding HTTP Authentication to a Middleman app using Heroku environment variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "rack" | |
require "middleman/rack" | |
require "rack/contrib/try_static" | |
protected_middleman = Rack::Auth::Basic.new(Middleman.server) do |username, password| | |
[username, password] == [ENV['HTTP_USERNAME'], ENV['HTTP_PASSWORD']] | |
# Enable proper HEAD responses | |
use Rack::Head | |
# Attempt to serve static HTML files | |
use Rack::TryStatic, | |
:root => "tmp", | |
:urls => %w[/], | |
:try => ['.html', 'index.html', '/index.html'] | |
# Serve a 404 page if all else fails | |
run lambda { |env| | |
[ | |
404, | |
{ | |
"Content-Type" => "text/html", | |
"Cache-Control" => "public, max-age=60" | |
}, | |
File.open("tmp/404/index.html", File::RDONLY) | |
] | |
} | |
end | |
# Build the static site when the app boots | |
`bundle exec middleman build` | |
run protected_middleman |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment