Skip to content

Instantly share code, notes, and snippets.

@snapper
Created March 2, 2012 09:11
Show Gist options
  • Save snapper/1957045 to your computer and use it in GitHub Desktop.
Save snapper/1957045 to your computer and use it in GitHub Desktop.
A sample config.ru file
# 301 rewrite!
require 'rack/rewrite'
use Rack::Rewrite do
r301 %r{.*}, 'http://somedomain.com$&', :if => Proc.new {|rack_env|
rack_env['SERVER_NAME'] != 'somedomain.com'
}
end
# Attribution goes to https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/try_static.rb
# License can be found here https://github.com/rack/rack-contrib/blob/master/COPYING
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
# Set up static serving
use Rack::TryStatic, :root => "_site", :urls => %w[/], :try => ['.html', 'index.html', '/index.html']
# Serve the 404 error page
error_file = '_site/404.html'
run lambda { |env|
[404, {
'Last-Modified' => File.mtime(error_file).httpdate,
'Content-Type' => 'text/html',
'Content-Length' => File.size(error_file)
},
File.read(error_file) ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment