Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a static file middleware that will pass through to the applicatio…
…n if the file does not exist. This is based on the Rails::Rack::Static middleware from Rails 3. Making this change will enable serving files from any path (not just under 'public', and allow creation of files such as robots.txt.
- Loading branch information
Showing
5 changed files
with
39 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'rack/utils' | ||
|
||
# Heavily based on ActionDispatch::Static | ||
module Vanilla | ||
class Static | ||
def initialize(app, root) | ||
@app = app | ||
@file_server = ::Rack::File.new(root) | ||
end | ||
|
||
def call(env) | ||
path = env['PATH_INFO'].chomp('/') | ||
method = env['REQUEST_METHOD'] | ||
|
||
if %w(GET HEAD).include?(method) && file_exist?(path) | ||
@file_server.call(env) | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
|
||
private | ||
def file_exist?(path) | ||
full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path)) | ||
File.file?(full_path) && File.readable?(full_path) | ||
end | ||
end | ||
end |
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