Mailinglist Archive: yast-devel (246 mails)

< Previous Next >
[yast-devel] RFC: plugin static assets
  • From: "Duncan Mac-Vicar Prett" <dmacvicar@xxxxxxx>
  • Date: Wed, 26 Aug 2009 15:35:31 +0200
  • Message-id: <200908261535.32094.dmacvicar@xxxxxxx>

Hi

By default, rails engines do not support static assets in their own public/
directory. It is a so obvious feature that I had the prejudge in mind that it
would not be trivial.

I implemented a Rack middleware that looks in all the loaded plugins for a
resource, if it can be served from there, it does it, if not, it forwards to
the application (a simple overlay, just like the other parts of the engines
work).

Attached is the patch for review.

This allows engines to have their own public/ directory with files, images,
css files, etc. Just like with engines controllers and views, you need to have
name conventions, as the names may clash.

--
Duncan Mac-Vicar P. - Engineering Manager, YaST
SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nuernberg)

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
< Previous Next >
This Thread
  • No further messages