Last active
December 26, 2015 19:19
-
-
Save rafael/7200323 to your computer and use it in GitHub Desktop.
statics file server rack.
This file contains hidden or 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 '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