Skip to content

Instantly share code, notes, and snippets.

@StGerman
Last active August 29, 2015 14:06
Show Gist options
  • Save StGerman/c324ace54b9e41a8f47d to your computer and use it in GitHub Desktop.
Save StGerman/c324ace54b9e41a8f47d to your computer and use it in GitHub Desktop.

Install

  • Create each of these files in a single folder
  • bundle install

Development

  • middleman

You need to restart middleman after each change in dynamic_app.rb The alternative for development is to put everything in config.rb but that's not really pretty.

Testing in "compiled mode" (Sometimes you want to do this)

  • middleman build
  • foreman start

After that, don't forget to rm -fr ./tmp

Deploying to Heroku:

Nothing special, plain old ruby deployment: git push heroku master

The deployment process will do a rake assets:precompile step that will build the middleman site. The config.ru serves static files first if they're found, so any file that was built statically will be served, otherwise, it'll fall back to your Sinatra app. Ain't that pretty?

require 'dynamic_app'
set :build_dir, 'tmp'
# ... Rest of your middleman config
use DynamicApp
require 'rack'
require 'try_static'
require 'dynamic_app'
# First, `use` our hullapp middleware
use DynamicApp
# Serve Static Files
use Rack::TryStatic, :root => 'tmp', :urls => %w[/], :try => ['.html', 'index.html', '/index.html']
# Minimal 404
run lambda { |env|
not_found_page = File.expand_path('../build/404/index.html', __FILE__)
if File.exist?(not_found_page)
[ 404, { 'Content-Type' => 'text/html'}, [File.read(not_found_page)] ]
else
[ 404, { 'Content-Type' => 'text/html' }, ['404 - page not found'] ]
end
}
require 'sinatra'
require 'sinatra/base'
class DynamicApp < Sinatra::Base
# Tweak to your needs
set :static, true
set :public_folder, File.dirname(__FILE__) + '/tmp'
post '/reviews' do
# Do your dynamic things here.
end
end
source 'http://rubygems.org'
##################
# Rack
##################
gem 'puma'
gem 'rake'
gem 'rack-contrib'
##################
# Middleman
##################
gem 'middleman'
gem 'middleman-sprockets'
gem 'middleman-smusher'
gem 'middleman-minify-html'
gem 'middleman-livereload'
# Below this is: Not necessary, but what I like to have in an app :)
##################
# Data
##################
gem 'hullio'
gem 'builder'
gem 'hashie'
gem 'activesupport'
gem 'awesome_print'
##################
# Assets
##################
gem 'uglifier'
gem 'slim'
gem 'sanitize'
gem 'handlebars_assets'
gem 'sass', '~> 3.2.5'
gem 'compass', :git => 'git://github.com/chriseppstein/compass.git', :tag => 'v0.13.alpha.4'
gem 'sass-getunicode'
gem 'bootstrap-sass', '3.0.3.0'
##################
# JS
##################
gem 'execjs'
gem 'json', '~> 1.8.0'
gem 'oj' # faster JSON
##################
# Fixtures
##################
gem 'uifaces'
gem 'random_data'
##################
# Build Targets
##################
group :development do
gem 'foreman'
gem 'fontcustom'
end
web: bundle exec puma -p $PORT
namespace :assets do
task :precompile do
sh 'middleman build'
end
end
# Customized version of the TryStatic standard rack.
module Rack
class TryStatic
def initialize(app, options)
@app = app
@try = ['', *options.delete(:try)]
@static = ::Rack::Static.new(lambda { [404, {}, []] }, options)
end
def call(env)
orig_path = env['PATH_INFO']
found = nil
@try.each do |path|
resp = @static.call(env.merge!({'PATH_INFO' => orig_path + path}))
break if 404 != resp[0] && found = resp
end
found or @app.call(env.merge!('PATH_INFO' => orig_path))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment