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 .html)
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.



27
28
29
# File 'lib/simple/httpd/rack/static_mount.rb', line 27

def mount_point
  @mount_point
end

#pathObject (readonly)

Returns the value of attribute path.



27
28
29
# File 'lib/simple/httpd/rack/static_mount.rb', line 27

def path
  @path
end

Class Method Details

.build(mount_point, path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

  if ::Simple::Httpd.logger.debug?
    static_files.sort.each do |file|
      ::Simple::Httpd.logger.debug "#{mount_point}/#{file}"
    end
  end

  new(mount_point, path, static_files)
end

Instance Method Details

#call(env) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/simple/httpd/rack/static_mount.rb', line 46

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