low_loop 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/factories/response_factory.rb +14 -12
- data/lib/low_loop.rb +42 -46
- data/lib/responses/file_response.rb +2 -2
- data/lib/servers/file_server.rb +3 -3
- data/lib/states/file_state.rb +2 -7
- data/lib/version.rb +3 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3bd2e532e273f35fcbcd9d8e9f0f4b0d9261e536d293908026371412ec11573e
|
|
4
|
+
data.tar.gz: 39b83d8235d14d0d7359ea742f1e9c3d4ab4bdd1e0cceca01006950d2d942cc6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 17b1927035f2900208d1cbe6399b7814ec812f19afa1b22734e83995fd7e8f5bbf5d6724403553c55a83ef0779cf7b7bcaa56e56763ee8e2b67814980fab2b53
|
|
7
|
+
data.tar.gz: 29b8756fbb3957e9ab50a5748391b745ca244af98cea79ae2d3e9ed8955ec07aa3f7aad9d339a6154270e004b2058592272a8db132717354c6f5af200245f017
|
|
@@ -4,21 +4,23 @@ require 'protocol/http'
|
|
|
4
4
|
require 'protocol/http/body/file'
|
|
5
5
|
|
|
6
6
|
module Low
|
|
7
|
-
|
|
8
|
-
class
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
module Factories
|
|
8
|
+
class ResponseFactory
|
|
9
|
+
class << self
|
|
10
|
+
def html(body:)
|
|
11
|
+
headers = Protocol::HTTP::Headers.new(['content-type', 'text/html'])
|
|
12
|
+
body = Protocol::HTTP::Body::Buffered.wrap(body)
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
Protocol::HTTP::Response.new('http/1.1', 200, headers, body)
|
|
15
|
+
end
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
def file(path:, content_type:)
|
|
18
|
+
headers = Protocol::HTTP::Headers.new(['content-type', content_type])
|
|
19
|
+
file = File.open(path, 'rb')
|
|
20
|
+
body = Protocol::HTTP::Body::File.new(file)
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
Protocol::HTTP::Response.new('http/1.1', 200, headers, body)
|
|
23
|
+
end
|
|
22
24
|
end
|
|
23
25
|
end
|
|
24
26
|
end
|
data/lib/low_loop.rb
CHANGED
|
@@ -10,66 +10,62 @@ require_relative 'factories/response_factory'
|
|
|
10
10
|
require_relative 'requests/request_parser'
|
|
11
11
|
require_relative 'responses/response_builder'
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
include Observers
|
|
13
|
+
class LowLoop
|
|
14
|
+
include Observers
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
attr_reader :config
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
def initialize(config:, dependencies: [])
|
|
19
|
+
@config = config
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
observers.push(self, action: :mirror) if config.mirror_mode
|
|
21
|
+
dependencies.each do |dependency|
|
|
22
|
+
observers << dependency
|
|
27
23
|
end
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
observers.push(self, action: :mirror) if config.mirror_mode
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def start
|
|
29
|
+
server = start_server
|
|
31
30
|
|
|
32
|
-
|
|
31
|
+
Fiber.set_scheduler(Async::Scheduler.new)
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
Fiber.schedule do
|
|
34
|
+
loop do
|
|
35
|
+
socket = server.accept
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
Fiber.schedule do
|
|
38
|
+
request = Low::RequestParser.parse(socket:, host: config.host, port: config.port)
|
|
39
|
+
response_event = take(event: Low::Events::RequestEvent.new(request:))
|
|
40
|
+
response = response_event.response
|
|
42
41
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
end
|
|
42
|
+
Low::ResponseBuilder.respond(config:, socket:, response:)
|
|
43
|
+
rescue StandardError => e
|
|
44
|
+
puts e.message
|
|
45
|
+
ensure
|
|
46
|
+
socket&.close
|
|
49
47
|
end
|
|
50
48
|
end
|
|
51
49
|
end
|
|
50
|
+
end
|
|
52
51
|
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
def start_server
|
|
53
|
+
puts "Starting server @ #{config.host}:#{config.port}" unless config.matrix_mode
|
|
55
54
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
# Fallback mode for when there's no dependencies and you want to know that the server is still working.
|
|
62
|
-
def mirror(event:)
|
|
63
|
-
request = event.request
|
|
64
|
-
response = ResponseFactory.html(body: "Thank you for visiting #{request.path} with body: '#{request.body}'")
|
|
65
|
-
Events::ResponseEvent.new(response:)
|
|
66
|
-
end
|
|
55
|
+
server = TCPServer.new(config.host, config.port)
|
|
56
|
+
server.listen(10)
|
|
57
|
+
server
|
|
58
|
+
end
|
|
67
59
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
60
|
+
# Fallback mode for when there's no dependencies and you want to know that the server is still working.
|
|
61
|
+
def mirror(event:)
|
|
62
|
+
request = event.request
|
|
63
|
+
response = Low::Factories::ResponseFactory.html(body: "Thank you for visiting #{request.path} with body: '#{request.body}'")
|
|
64
|
+
Low::Events::ResponseEvent.new(response:)
|
|
72
65
|
end
|
|
73
|
-
end
|
|
74
66
|
|
|
75
|
-
LowLoop
|
|
67
|
+
# Consider LowLoop a value object in the context of Observers (there can only be one).
|
|
68
|
+
def ==(other) = other.class == self.class
|
|
69
|
+
def eql?(other) = self == other
|
|
70
|
+
def hash = [self.class].hash
|
|
71
|
+
end
|
|
@@ -9,8 +9,8 @@ module Low
|
|
|
9
9
|
file = event.file
|
|
10
10
|
|
|
11
11
|
if File.exist?(file.path)
|
|
12
|
-
response = ResponseFactory.file(path: file.path, content_type: file.content_type)
|
|
13
|
-
return
|
|
12
|
+
response = Factories::ResponseFactory.file(path: file.path, content_type: file.content_type)
|
|
13
|
+
return Events::ResponseEvent.new(response:)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
nil
|
data/lib/servers/file_server.rb
CHANGED
|
@@ -15,7 +15,7 @@ module Low
|
|
|
15
15
|
@web_root = web_root
|
|
16
16
|
@content_types = content_types.transform_keys(&:to_s)
|
|
17
17
|
|
|
18
|
-
observers(
|
|
18
|
+
observers(Events::FileEvent) << FileResponse
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def extension(filepath:)
|
|
@@ -34,9 +34,9 @@ module Low
|
|
|
34
34
|
extension = extension(filepath:)
|
|
35
35
|
return nil if extension.nil?
|
|
36
36
|
|
|
37
|
-
file = FileState.new(path: safe_path(filepath), content_type: @content_types[extension])
|
|
37
|
+
file = States::FileState.new(path: safe_path(filepath), content_type: @content_types[extension])
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
Events::FileEvent.trigger(file:, request: event.request)
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
private
|
data/lib/states/file_state.rb
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Low
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def initialize(path:, content_type:)
|
|
8
|
-
@path = path
|
|
9
|
-
@content_type = content_type
|
|
10
|
-
end
|
|
4
|
+
module States
|
|
5
|
+
FileState = Data.define(:path, :content_type)
|
|
11
6
|
end
|
|
12
7
|
end
|
data/lib/version.rb
CHANGED