module Roda::RodaPlugins::MultiPublic

  1. lib/roda/plugins/multi_public.rb

The multi_public plugin adds an r.multi_public method that accepts an argument specifying a directory from which to serve static files. It is similar to the public plugin, but allows for multiple separate directories.

Here’s an example of using the multi_public plugin to serve 3 different types of files from 3 different directories:

plugin :multi_public,
  img:  'static/images',
  font: 'assets/fonts',
  form: 'static/forms/pdfs'

r.route do
  r.on "images" do
    r.multi_public(:img)
  end

  r.on "fonts" do
    r.multi_public(:font)
  end

  r.on "forms" do
    r.multi_public(:form)
  end
end

It is possible to simplify the routing tree for this using string keys and an array matcher:

plugin :multi_public,
  'images' => 'static/images',
  'fonts'  => 'assets/fonts',
  'forms'  => 'static/forms/pdfs'

r.route do
  r.on %w"images fonts forms" do |dir|
    r.multi_public(dir)
  end
end

You can provide custom headers and default mime type for each directory using an array of three elements as the value, with the first element being the path, the second being the custom headers, and the third being the default mime type:

plugin :multi_public,
  'images' => ['static/images', {'Cache-Control'=>'max-age=86400'}, nil],
  'fonts'  => ['assets/fonts', {'Cache-Control'=>'max-age=31536000'}, 'font/ttf'],
  'forms'  => ['static/forms/pdfs', nil, 'application/pdf']

r.route do
  r.on %w"images fonts forms" do |dir|
    r.multi_public(dir)
  end
end

Methods

Public Class

  1. configure
  2. load_dependencies

Constants

RACK_FILES = defined?(Rack::Files) ? Rack::Files : Rack::File  

Public Class methods

configure(app, directories, _=OPTS)

Use the given directories to setup servers. Any opts are passed to the public plugin.

[show source]
   # File lib/roda/plugins/multi_public.rb
74 def self.configure(app, directories, _=OPTS)
75   roots = app.opts[:multi_public_servers] = (app.opts[:multi_public_servers] || {}).dup
76   directories.each do |key, path|
77     path, headers, mime = path
78     roots[key] = RACK_FILES.new(app.expand_path(path), headers||{}, mime||'text/plain')
79   end
80   roots.freeze
81 end
load_dependencies(app, _, opts=OPTS)
[show source]
   # File lib/roda/plugins/multi_public.rb
69 def self.load_dependencies(app, _, opts=OPTS)
70   app.plugin(:public, opts)
71 end