Class: LowLoop
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Consider LowLoop a value object in the context of Observers (there can only be one).
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(config:, router: nil, renderer: nil, show_output: true) ⇒ LowLoop
constructor
A new instance of LowLoop.
-
#mirror(event:) ⇒ Object
Fallback mode for when there’s no dependencies and you want to know that the server is still working.
- #start ⇒ Object
- #start_server ⇒ Object
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 |
# File 'lib/low_loop.rb', line 20 def initialize(config:, router: nil, renderer: nil, show_output: true) @config = config @frame = LowFrame.new(renderer:, fps: 30, show_output:) observers(Low::Events::RequestEvent) << Low::FileServer.new(web_root: config.web_root, content_types: config.content_types) observers(Low::Events::RequestEvent) << router if router observers.push(Low::Events::RequestEvent, action: :mirror) if config.mirror_mode 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).
72 |
# File 'lib/low_loop.rb', line 72 def ==(other) = other.class == self.class |
#eql?(other) ⇒ Boolean
73 |
# File 'lib/low_loop.rb', line 73 def eql?(other) = self == other |
#hash ⇒ Object
74 |
# File 'lib/low_loop.rb', line 74 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.
65 66 67 68 69 |
# File 'lib/low_loop.rb', line 65 def mirror(event:) request = event.request response = Low::Factories::ResponseFactory.html(body: "Thank you for visiting #{request.path} with body: '#{request.body}'") Low::Events::ResponseEvent.new(response:) end |
#start ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/low_loop.rb', line 30 def start server = start_server Fiber.set_scheduler(Async::Scheduler.new) Fiber.schedule do loop do socket = server.accept @frame.render if @frame.renderer Fiber.schedule do request = Low::RequestParser.parse(socket:, host: config.host, port: config.port) response_event = Low::Events::RequestEvent.take(request:) response = response_event.response Low::ResponseBuilder.respond(config:, socket:, response:) rescue StandardError => e puts e. ensure socket&.close end end end end |
#start_server ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/low_loop.rb', line 56 def start_server puts "Starting server @ #{config.host}:#{config.port}" unless config.matrix_mode server = TCPServer.new(config.host, config.port) server.listen(10) server end |