Last active
February 25, 2016 17:21
-
-
Save mwpastore/18ca324c3326584758e0 to your computer and use it in GitHub Desktop.
Sinatra example for ember-cli-deploy-mysql
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 'sinatra' | |
require 'sequel' | |
require 'mysql2' | |
configure do | |
# Define your database, table, and revision preview query parameter here! | |
set :database, Sequel.connect ENV.fetch('DATABASE_URL') | |
set :table, :foo_bootstrap | |
set :param_key, 'preview' | |
end | |
helpers do | |
def fetch_index | |
stack = [] | |
indexes = settings.database | |
.from(settings.table) | |
.to_hash(:key, :value) | |
stack.push params.fetch(settings.param_key, 'current') | |
while value = indexes[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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also ember-cli-deploy-redis-sinatra.rb.