Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Joseworks/e37b8cef884105da2933c78d1c45fccd to your computer and use it in GitHub Desktop.
Save Joseworks/e37b8cef884105da2933c78d1c45fccd to your computer and use it in GitHub Desktop.
Adding HTTP Authentication to a Middleman app using Heroku environment variables
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