Skip to content

Instantly share code, notes, and snippets.

@rafael
Last active December 26, 2015 19:19
Show Gist options
  • Select an option

  • Save rafael/7200323 to your computer and use it in GitHub Desktop.

Select an option

Save rafael/7200323 to your computer and use it in GitHub Desktop.
statics file server rack.
require 'rack/utils'
class StaticRack
def call(env)
path = env["SERVER_NAME"]
file, headers = pick_file_and_headers_for_path(path, env)
file_server = ::Rack::File.new(file, headers)
file_server.call(env)
end
private
def get_best_encoding(request)
Rack::Utils.select_best_encoding(%w(gzip identity), request.accept_encoding)
end
def headers
@headers ||= {'Content-Type' => 'text/html' }
end
def pick_file_and_headers_for_path(path, env)
request = Rack::Request.new(env)
encoding = get_best_encoding(request)
file_path = "#{Rails.root.to_s}/public/media_kits/" + path + '.html'
if (File.exist?(file_path))
if encoding == 'gzip'
file_path = file_path + '.gz'
headers['Content-Encoding'] = 'gzip'
end
else
file_path = "#{Rails.root.to_s}/public/404.html"
end
[file_path, headers]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment