-
-
Save adamhunter/175550 to your computer and use it in GitHub Desktop.
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
init = Rails::Initializer.run do |config| | |
#... | |
end | |
# look for all existing loaded plugin's public/ directories | |
plugin_assets = init.loaded_plugins.map { |plugin| File.join(plugin.directory, 'public') }.reject { |dir| not (File.directory?(dir) and File.exist?(dir)) } | |
init.configuration.middleware.use MyApp::Rack::StaticOverlay, :roots => plugin_assets |
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
module MyApp | |
module Rack | |
# class that looks for a static request in a list | |
# of directories. If the file can't be served from any | |
# of the overlays | |
# then the request is forwarded to the application | |
class StaticOverlay | |
# initialize the middleware | |
# known options: | |
# :roots => [ dir, ... ] | |
def initialize(app, options={}) | |
@app = app | |
@servers = {} | |
@roots = options[:roots] || [] | |
@roots.each do |root| | |
@servers[root] = ::Rack::File.new(root) | |
end | |
end | |
def call(env) | |
req = ::Rack::Request.new(env) | |
resource = URI.parse(req.url).path | |
puts resource | |
# go over all overlays | |
@roots.each do |directory| | |
resource_path = File.join(directory, resource) | |
if File.exist?(resource_path) and File.file?(resource_path) | |
return @servers[directory].call(env) | |
end | |
end | |
# if the asset was nowhere, forward | |
return @app.call(env) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment