Skip to content

Instantly share code, notes, and snippets.

@mwpastore
Last active February 25, 2016 17:20
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 mwpastore/2285c51012eb4bd8a817 to your computer and use it in GitHub Desktop.
Save mwpastore/2285c51012eb4bd8a817 to your computer and use it in GitHub Desktop.
A slightly more sophisticated Sinatra example for ember-cli-deploy
require 'sinatra'
require 'redis'
configure do
# Define your Redis connection, key prefix, and revision preview query parameter here!
set :redis, Redis.new
set :prefix, 'foo:index'
set :param_key, 'preview'
end
helpers do
def fetch_index
stack = []
stack.push params.fetch(settings.param_key, 'current')
while value = settings.redis.get("#{settings.prefix}:#{stack.last}")
return [value, stack.last] if value =~ /\s/
break if stack.include? value # cycle guard
stack.push value
end
not_found 'Invalid Site Index'
end
def extract_http_equiv!(content)
modified_content = content.dup
content.scan(/<\s*meta\s+http-equiv="?([\w-]+)"?\s+content="?(.+?)"?\s*>/im) do |pairs|
yield pairs
modified_content.slice! $&
end
content.replace modified_content
end
end
get '/', :provides => :html do
index, revision = fetch_index
etag revision
expires 3_600, :public
extract_http_equiv!(index) do |key, value|
response[key] = value
end
index.gsub(/\n */, '')
end
@mwpastore
Copy link
Author

It's probably overkill, but this supports multiple levels of index indirection (e.g. staging => current => abcd1234) and ETags for cache control. Based off of the original. It also extracts http-equiv meta tags from the index content and presents them as HTTP response headers instead, which is useful for certain CSP options. See also ember-cli-deploy-mysql-sinatra.rb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment