Skip to content

Instantly share code, notes, and snippets.

@acook
Last active December 21, 2015 04:18
Show Gist options
  • Save acook/6248045 to your computer and use it in GitHub Desktop.
Save acook/6248045 to your computer and use it in GitHub Desktop.
Serving up static files outside of Public with Sinatra. Good for putting static files behind your auth wall.
require 'pathname'
require 'sinatra/base'
module MyApp
module_function
def root
Pathname.new(__FILE__).dirname
end
end
class StaticEngine < Sinatra::Base
PATH = MyApp.root.join('static')
get %r{/static/(.*)} do
filename = params[:captures].first
fullpath = PATH.join(filename).expand_path
# Checks to make sure the requested path is a subdirectory of 'static'
if /^#{PATH}/ =~ fullpath.to_s then
send_file fullpath
else
halt 404
end
end
end
class App < Sinatra::Base
use StaticEngine
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment