Last active
December 21, 2015 04:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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