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: 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.



21
22
23
24
25
# File 'lib/yarrow/server.rb', line 21

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

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/yarrow/server.rb', line 27

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