Class: Utopia::Middleware::DirectoryIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/middleware/directory_index.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ DirectoryIndex

Returns a new instance of DirectoryIndex.



27
28
29
30
31
32
33
34
# File 'lib/utopia/middleware/directory_index.rb', line 27

def initialize(app, options = {})
  @app = app
  @root = options[:root] || Utopia::Middleware::default_root

  @files = ["index.html"]

  @default = "index"
end

Instance Method Details

#call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/utopia/middleware/directory_index.rb', line 36

def call(env)
  path = Path.create(env["PATH_INFO"])
  
  if path.directory?
    # Check to see if one of the files exists in the requested directory
    @files.each do |file|
      if File.exist?(File.join(@root, path.components, file))
        return [307, {"Location" => (path + file).to_s}, []]
      end
    end
  
    # Use the default path
    return [307, {"Location" => (path + @default).to_s}, []]
  else
    return @app.call(env)
  end
end