Class: Yarrow::Server::DirectoryIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/yarrow/server.rb

Overview

Rack Middleware for detecting and serving an ‘index.html’ file instead of a directory index.

TODO: Fix bug where a directory /index.html/ causes this to crash TODO: Add configurable mapping and media types for README files as an alternative

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DirectoryIndex.



24
25
26
27
28
# File 'lib/yarrow/server.rb', line 24

def initialize(app, options={})
  @app = app
  @root = options[:root]
  @index_file = options[:index]
end

Instance Method Details

#call(env) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/yarrow/server.rb', line 30

def call(env)
  index_path = File.join(@root, Rack::Request.new(env).path.split('/'), @index_file)
  if File.exist?(index_path)
    return [200, {"Content-Type" => "text/html"}, [File.read(index_path)]]
  else
    @app.call(env)
  end
end