Class: Rack::Static
- Inherits:
-
Object
- Object
- Rack::Static
- Defined in:
- lib/rack/static.rb
Overview
The Rack::Static middleware intercepts requests for static files (javascript files, images, stylesheets, etc) based on the url prefixes or route mappings passed in the options, and serves them using a Rack::File object. This allows a Rack stack to serve both static and dynamic content.
Examples:
Serve all requests beginning with /media from the “media” folder located in the current directory (ie media/*):
use Rack::Static, :urls => ["/media"]
Serve all requests beginning with /css or /images from the folder “public” in the current directory (ie public/css/* and public/images/*):
use Rack::Static, :urls => ["/css", "/images"], :root => "public"
Serve all requests to / with “index.html” from the folder “public” in the current directory (ie public/index.html):
use Rack::Static, :urls => {"/" => 'index.html'}, :root => 'public'
Set a fixed Cache-Control header for all served files:
use Rack::Static, :root => 'public', :cache_control => 'public'
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Static
constructor
A new instance of Static.
Constructor Details
#initialize(app, options = {}) ⇒ Static
Returns a new instance of Static.
32 33 34 35 36 37 38 |
# File 'lib/rack/static.rb', line 32 def initialize(app, ={}) @app = app @urls = [:urls] || ["/favicon.ico"] root = [:root] || Dir.pwd cache_control = [:cache_control] @file_server = Rack::File.new(root, cache_control) end |
Instance Method Details
#call(env) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rack/static.rb', line 40 def call(env) path = env["PATH_INFO"] unless @urls.kind_of? Hash can_serve = @urls.any? { |url| path.index(url) == 0 } else can_serve = @urls.key? path end if can_serve env["PATH_INFO"] = @urls[path] if @urls.kind_of? Hash @file_server.call(env) else @app.call(env) end end |