Parent

Class Index [+]

Quicksearch

Rack::Static

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'

Serve all requests normally from the folder “public” in the current directory but uses index.html as default route for “/“

    use Rack::Static, :urls => [""], :root => 'public', :index =>
    'public/index.html'

Set a fixed Cache-Control header for all served files:

    use Rack::Static, :root => 'public', :cache_control => 'public'

Public Class Methods

new(app, options={}) click to toggle source
    # File lib/rack/static.rb, line 38
38:     def initialize(app, options={})
39:       @app = app
40:       @urls = options[:urls] || ["/favicon.ico"]
41:       @index = options[:index]
42:       root = options[:root] || Dir.pwd
43:       cache_control = options[:cache_control]
44:       @file_server = Rack::File.new(root, cache_control)
45:     end

Public Instance Methods

call(env) click to toggle source
    # File lib/rack/static.rb, line 59
59:     def call(env)
60:       path = env["PATH_INFO"]
61: 
62:       if can_serve(path)
63:         env["PATH_INFO"] = (path == '/' ? @index : @urls[path]) if overwrite_file_path(path)
64:         @file_server.call(env)
65:       else
66:         @app.call(env)
67:       end
68:     end
can_serve(path) click to toggle source
    # File lib/rack/static.rb, line 55
55:     def can_serve(path)
56:       route_file(path) || overwrite_file_path(path)
57:     end
overwrite_file_path(path) click to toggle source
    # File lib/rack/static.rb, line 47
47:     def overwrite_file_path(path)
48:       @urls.kind_of?(Hash) && @urls.key?(path) || @index && path == '/'
49:     end
route_file(path) click to toggle source
    # File lib/rack/static.rb, line 51
51:     def route_file(path)
52:       @urls.kind_of?(Array) && @urls.any? { |url| path.index(url) == 0 }
53:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.