Class: Low::FileServer

Inherits:
Object
  • Object
show all
Includes:
LowType, Observers
Defined in:
lib/servers/file_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(web_root:, content_types:) ⇒ FileServer

Returns a new instance of FileServer.



14
15
16
17
18
19
# File 'lib/servers/file_server.rb', line 14

def initialize(web_root:, content_types:)
  @web_root = web_root
  @content_types = content_types.transform_keys(&:to_s)

  observers(Events::FileEvent) << FileResponse
end

Instance Method Details

#extension(filepath:) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/servers/file_server.rb', line 21

def extension(filepath:)
  extension = File.extname(filepath).delete_prefix('.')

  return nil if extension == ''
  return nil unless @content_types.key?(extension)

  extension
end

#handle(event:) ⇒ Object

TODO: Define type: Events::RequestEvent



31
32
33
34
35
36
37
38
39
40
# File 'lib/servers/file_server.rb', line 31

def handle(event:)
  filepath = Protocol::URL[event.request.path].path

  extension = extension(filepath:)
  return nil if extension.nil?

  file = States::FileState.new(path: safe_path(filepath), content_type: @content_types[extension])

  Events::FileEvent.trigger(file:, request: event.request)
end