Class: LowLoop

Inherits:
Object
  • Object
show all
Includes:
Observers
Defined in:
lib/low_loop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, router: nil, renderer: nil, show_output: true) ⇒ LowLoop

Returns a new instance of LowLoop.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/low_loop.rb', line 20

def initialize(config:, router: nil, renderer: nil, show_output: true)
  @config = config
  @connection_manager = Low::ConnectionManager.new(config:)
  @frame = LowFrame.new(renderer:, fps: 10, show_output:)

  Low::Events::RequestEvent.define do |observers|
    observers << Low::FileServer.new(web_root: config.web_root, content_types: config.content_types)
    observers << router if router
    observers.push(self, action: :mirror) if config.mirror_mode
  end
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/low_loop.rb', line 18

def config
  @config
end

Instance Method Details

#==(other) ⇒ Object

Consider LowLoop a value object in the context of Observers (there can only be one).



60
# File 'lib/low_loop.rb', line 60

def ==(other) = other.class == self.class

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


61
# File 'lib/low_loop.rb', line 61

def eql?(other) = self == other

#hash ⇒ Object



62
# File 'lib/low_loop.rb', line 62

def hash = [self.class].hash

#mirror(event:) ⇒ Object

Fallback mode for when there's no dependencies and you want to know that the server is still working.



53
54
55
56
57
# File 'lib/low_loop.rb', line 53

def mirror(event:)
  request = event.request
  response = tories::ResponseFactory.html(body: "Thank you for visiting #{request.path} with body: '#{request.body}'")
  Low::Events::ResponseEvent.new(response:)
end

#start ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/low_loop.rb', line 32

def start
  server = start_server

  Async do |task|
    render_frame(task)

    loop do
      socket = server.accept

      task.async do
        @connection_manager.handle_connection(socket:)
      rescue StandardError => e
        render_error(e)
      ensure
        socket&.close
      end
    end
  end
end