Class: Catapult::TryStatic
- Inherits:
-
Object
- Object
- Catapult::TryStatic
- Defined in:
- lib/catapult/try_static.rb
Overview
The Rack::TryStatic middleware delegates requests to Rack::Static middleware trying to match a static file
Examples
use Rack::TryStatic,
:root => "public", # static files root dir
:urls => %w[/], # match all requests
:try => ['.html', 'index.html', '/index.html'] # try these postfixes sequentially
uses same options as Rack::Static with extra :try option which is an array
of postfixes to find desired file
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options) ⇒ TryStatic
constructor
A new instance of TryStatic.
Constructor Details
#initialize(app, options) ⇒ TryStatic
Returns a new instance of TryStatic.
17 18 19 20 21 22 23 24 |
# File 'lib/catapult/try_static.rb', line 17 def initialize(app, ) @app = app @try = ['', *[:try]] @static = ::Rack::Static.new( lambda { |_| [404, {}, []] }, ) end |
Instance Method Details
#call(env) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/catapult/try_static.rb', line 26 def call(env) orig_path = env['PATH_INFO'] found = nil @try.each do |path| resp = @static.call(env.merge!({'PATH_INFO' => orig_path + path})) break if 404 != resp[0] && found = resp end found or @app.call(env.merge!('PATH_INFO' => orig_path)) end |