Skip to content

Instantly share code, notes, and snippets.

@matthewlehner
Created May 8, 2016 20:42
Show Gist options
  • Save matthewlehner/2dc7377c4207297ea628632cf0616256 to your computer and use it in GitHub Desktop.
Save matthewlehner/2dc7377c4207297ea628632cf0616256 to your computer and use it in GitHub Desktop.
class LetsEncryptResponse
ACME_ENDPOINT_REGEX = /\A\/\.well-known\/acme-challenge\/(?<challenge>.*)\z/
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new env
if request.path.start_with?("/.well-known/acme-challenge/")
match = request.path.match ACME_ENDPOINT_REGEX
challenge = match[:challenge]
if ENV["ACME_CHALLENGE"] && ENV["ACME_RESPONSE"] && challenge == ENV["ACME_CHALLENGE"]
[200, { "Content-Type" => "text/plain" }, [ENV["ACME_RESPONSE"]]]
else
[400, {}, ["This is a horrible error message."]]
end
else
@app.call env
end
end
end
Rails.config.middleware.insert_before ActionDispatch::SSL, LetsEncryptResponse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment