Skip to content

Instantly share code, notes, and snippets.

@dmacvicar
Created August 26, 2009 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmacvicar/175518 to your computer and use it in GitHub Desktop.
Save dmacvicar/175518 to your computer and use it in GitHub Desktop.
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
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