Class: Machined::Middleware::Static

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

Overview

Machined::Middleware::Static serves static files from the given directory. If no static file is found, the request gets passed on to the next middleware or application. It’s basically a simplified version of ActionDispatch::Static.

Instance Method Summary collapse

Constructor Details

#initialize(app, root = '.', headers = {}) ⇒ Static

Returns a new instance of Static.



11
12
13
14
15
16
# File 'lib/machined/middleware/static.rb', line 11

def initialize(app, root = '.', headers = {})
  @app = app
  @root = File.expand_path(root)
  @compiled_root = /^#{Regexp.escape(@root)}/
  @file_server = ::Rack::File.new(@root, headers)
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/machined/middleware/static.rb', line 18

def call(env)
  case env['REQUEST_METHOD']
  when 'GET', 'HEAD'
    path = env['PATH_INFO'].chomp('/')
    if match = match?(path)
      env['PATH_INFO'] = match
      return @file_server.call(env)
    end
  end

  @app.call(env)
end