ref: refs/heads/network
commit 944d2a36357282d9b424d82b88cf3bccc23b516f
Author: Ladislav Slezak
Date: Thu Sep 10 15:00:41 2009 +0200
added static overlay - rest plugins can export public/ subdir
---
webservice/config/environment.rb | 8 ++++-
webservice/lib/yast/rack/static_overlay.rb | 43 ++++++++++++++++++++++++++++
2 files changed, 50 insertions(+), 1 deletions(-)
diff --git a/webservice/config/environment.rb b/webservice/config/environment.rb
index c85bf30..5c00ea3 100644
--- a/webservice/config/environment.rb
+++ b/webservice/config/environment.rb
@@ -107,4 +107,10 @@ unless defined? PERMISSION_CHECK_TESTING
USER_ROLES_CONFIG = "/etc/yast_user_roles"
-end
\ No newline at end of file
+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)) }
+
+require 'yast/rack/static_overlay'
+init.configuration.middleware.use YaST::Rack::StaticOverlay, :roots => plugin_assets
diff --git a/webservice/lib/yast/rack/static_overlay.rb b/webservice/lib/yast/rack/static_overlay.rb
new file mode 100644
index 0000000..807470d
--- /dev/null
+++ b/webservice/lib/yast/rack/static_overlay.rb
@@ -0,0 +1,43 @@
+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