Class: StaticRails::StaticMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/static-rails/static_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ StaticMiddleware

Returns a new instance of StaticMiddleware.



8
9
10
11
12
# File 'lib/static-rails/static_middleware.rb', line 8

def initialize(app)
  @matches_request_to_static_site = MatchesRequestToStaticSite.new
  @app = app
  @file_handlers = {}
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/static-rails/static_middleware.rb', line 14

def call(env)
  return @app.call(env) unless StaticRails.config.serve_compiled_assets
  req = Rack::Request.new env

  if (req.get? || req.head?) && (site = @matches_request_to_static_site.call(req))
    file_handler = file_handler_for(site)
    path = req.path_info.gsub(/^#{site.url_root_path}/, "").chomp("/")
    if (result = serve_file_for(file_handler, site, path, req))
      return result
    end
  end

  @app.call(req.env)
end