ref: refs/heads/master
commit f8fc6dd7371b0069a0d90fc88965a634acba6757
Author: Duncan Mac-Vicar P
Date: Wed Aug 26 18:39:06 2009 +0200
- allow assets in plugins
---
webclient/config/environment.rb | 7 ++++
webclient/lib/yast/rack/static_overlay.rb | 44 +++++++++++++++++++++++++++++
2 files changed, 51 insertions(+), 0 deletions(-)
diff --git a/webclient/config/environment.rb b/webclient/config/environment.rb
index 100882e..81c4269 100644
--- a/webclient/config/environment.rb
+++ b/webclient/config/environment.rb
@@ -1,3 +1,5 @@
+require 'yast/rack/static_overlay'
+
# Be sure to restart your server when you modify this file
# Uncomment below to force Rails into production mode when
@@ -77,3 +79,8 @@ module YaST
end
YaST::LOADED_PLUGINS = init.loaded_plugins
+# 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 YaST::Rack::StaticOverlay, :roots => plugin_assets
+
+
diff --git a/webclient/lib/yast/rack/static_overlay.rb b/webclient/lib/yast/rack/static_overlay.rb
new file mode 100644
index 0000000..d5b7bf0
--- /dev/null
+++ b/webclient/lib/yast/rack/static_overlay.rb
@@ -0,0 +1,44 @@
+require 'rack/request'
+require 'uri'
+
+module YaST
+
+ 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
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org