Skip to content

Instantly share code, notes, and snippets.

@natew
Created July 24, 2013 17:28
Show Gist options
  • Save natew/6072646 to your computer and use it in GitHub Desktop.
Save natew/6072646 to your computer and use it in GitHub Desktop.
Turn any directory into a simple rack server to serve static files
# This is the root of our app
@root = File.expand_path(File.dirname(__FILE__))
run Proc.new { |env|
# Extract the requested path from the request
path = Rack::Utils.unescape(env['PATH_INFO'])
index_file = @root + "#{path}index.html"
if File.exists?(index_file)
# Return the index
[200, {'Content-Type' => 'text/html'}, [File.read(index_file)]]
else
# Pass the request to the directory app
Rack::Directory.new(@root).call(env)
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment