The public plugin adds a r.public
routing method to serve static files from a directory.
The public plugin recognizes the application’s :root option, and defaults to using the public
subfolder of the application’s :root
option. If the application’s :root option is not set, it defaults to the public
folder in the working directory. Additionally, if a relative path is provided as the :root
option to the plugin, it will be considered relative to the application’s :root
option.
Examples:
# Use public folder as location of files plugin :public # Use /path/to/app/static as location of files opts[:root] = '/path/to/app' plugin :public, root: 'static' # Assuming public is the location of files r.route do # Make GET /images/foo.png look for public/images/foo.png r.public # Make GET /static/images/foo.png look for public/images/foo.png r.on(:static) do r.public end end
Classes and Modules
Constants
PARSER | = | URI::DEFAULT_PARSER | ||
RACK_FILES | = | defined?(Rack::Files) ? Rack::Files : Rack::File | ||
SPLIT | = | Regexp.union(*[File::SEPARATOR, File::ALT_SEPARATOR].compact) |
Public Class methods
Use options given to setup a Rack::File instance for serving files. Options:
:default_mime |
The default mime type to use if the mime type is not recognized. |
:gzip |
Whether to serve already gzipped files with a .gz extension for clients supporting gzipped transfer encoding. |
:brotli |
Whether to serve already brotli-compressed files with a .br extension for clients supporting brotli transfer encoding. |
:headers |
A hash of headers to use for statically served files |
:root |
Use this option for the root of the public directory (default: “public”) |
# File lib/roda/plugins/public.rb 56 def self.configure(app, opts={}) 57 if opts[:root] 58 app.opts[:public_root] = app.expand_path(opts[:root]) 59 elsif !app.opts[:public_root] 60 app.opts[:public_root] = app.expand_path("public") 61 end 62 app.opts[:public_server] = RACK_FILES.new(app.opts[:public_root], opts[:headers]||{}, opts[:default_mime] || 'text/plain') 63 app.opts[:public_gzip] = opts[:gzip] 64 app.opts[:public_brotli] = opts[:brotli] 65 end