Skip to content

Instantly share code, notes, and snippets.

@stas
Created August 13, 2012 21:03
Show Gist options
  • Save stas/3344092 to your computer and use it in GitHub Desktop.
Save stas/3344092 to your computer and use it in GitHub Desktop.
Rack app for serving static files (ex. on Heroku), handles haml if any, just drop it in compass project dir.
require 'haml'
# Do not buffer output
$stdout.sync = true
# Get working dir, fixes issues when rackup is called outside app's dir
root_path = Dir.pwd
use Rack::Static,
:urls => ['/stylesheets', '/images', '/javascripts', '/fonts'],
:root => root_path
run lambda { |env|
request = Rack::Request.new(env)
path = request.path_info[1..-1]
path = 'index' if path.empty?
page_html = File.expand_path(path + '.html', root_path)
page_haml = File.expand_path(path + '.haml.html', root_path)
page = 'Not Found.'
code = 404
if File.exist?( page_html )
page = File.read(page_html)
code = 200
end
if File.exist?( page_haml )
template = File.read(page_haml)
page = Haml::Engine.new(template).render()
code = 200
end
[ code, {
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
[page] ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment