Class: Simple::Httpd::App::FileServer

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/httpd/app/file_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, url_prefix:, root:) ⇒ FileServer

A simple file server middleware



3
4
5
6
7
# File 'lib/simple/httpd/app/file_server.rb', line 3

def initialize(app, url_prefix:, root:)
  @app = app
  @url_prefix = File.join("/", url_prefix, "/")
  @file_server = Rack::File.new(root)
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/simple/httpd/app/file_server.rb', line 9

def call(env)
  request_path = env["PATH_INFO"]
  if request_path.start_with?(@url_prefix)
    file_path = request_path[@url_prefix.length..-1]
    env["PATH_INFO"] = file_path
    @file_server.call(env)
  else
    @app.call(env)
  end
end