Class: Tony::Static

Inherits:
Object
  • Object
show all
Defined in:
lib/tony/static.rb

Instance Method Summary collapse

Constructor Details

#initialize(app = Rack::NotFound.new, public_folder: 'public') ⇒ Static

Returns a new instance of Static.



6
7
8
9
# File 'lib/tony/static.rb', line 6

def initialize(app = Rack::NotFound.new, public_folder: 'public')
  @app = app
  @file_server = Rack::File.new(public_folder)
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/tony/static.rb', line 11

def call(env)
  req = Rack::Request.new(env)
  return @app.call(env) unless req.get?

  status, headers, body = @file_server.call(env)
  return @app.call(env) if status == 404

  headers['Cache-Control'] = 'public, max-age=31536000, immutable'
  return [status, headers, body]
end