Class: Simple::Httpd::Rack::StaticMount

Inherits:
Object
  • Object
show all
Includes:
Simple::Httpd::RouteDescriptions
Defined in:
lib/simple/httpd/rack/static_mount.rb

Overview

A simple file server middleware

Constant Summary collapse

H =
::Simple::Httpd::Helpers
Rack =
::Simple::Httpd::Rack
EXTENSIONS =
%w(.txt .md .js .css .png .jpeg .jpg)
GLOB_PATTERN =
"**/*.{#{EXTENSIONS.map { |s| s[1..-1] }.join(",")}}"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Simple::Httpd::RouteDescriptions

#describe_route!, #route_descriptions

Instance Attribute Details

#mount_pointObject (readonly)

Returns the value of attribute mount_point.



21
22
23
# File 'lib/simple/httpd/rack/static_mount.rb', line 21

def mount_point
  @mount_point
end

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/simple/httpd/rack/static_mount.rb', line 21

def path
  @path
end

Class Method Details

.build(mount_point, path) ⇒ Object



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

def self.build(mount_point, path)
  static_files = Dir.chdir(path) { Dir.glob(GLOB_PATTERN) }

  return nil if static_files.empty?

  ::Simple::Httpd.logger.info do
    "#{mount_point}: serving #{static_files.count} static file(s)"
  end

  new(mount_point, path, static_files)
end

Instance Method Details

#call(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/simple/httpd/rack/static_mount.rb', line 40

def call(env)
  request_path = env["PATH_INFO"]
  if serve_file?(request_path)
    file_path = request_path[1..-1]
    env["PATH_INFO"] = file_path
    @file_server.call(env)
  else
    Rack.error 404, "No such file"
  end
end