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

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
66 def self.configure(app, directories, _=OPTS)
67   roots = app.opts[:multi_public_servers] = (app.opts[:multi_public_servers] || {}).dup
68   directories.each do |key, path|
69     path, headers, mime = path
70     roots[key] = ::Rack::File.new(app.expand_path(path), headers||{}, mime||'text/plain')
71   end
72   roots.freeze
73 end
load_dependencies(app, _, opts=OPTS)
[show source]
   # File lib/roda/plugins/multi_public.rb
61 def self.load_dependencies(app, _, opts=OPTS)
62   app.plugin(:public, opts)
63 end