es-readmodel 0.1.0 → 1.0.0
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/README.md +3 -4
- data/es-readmodel.gemspec +1 -1
- data/lib/es_readmodel/rack_subscriber.rb +54 -0
- data/lib/es_readmodel/subscriber.rb +5 -41
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c07024ef0f57f993a1a7473791442ee8f1e98f97
|
4
|
+
data.tar.gz: f31b63a404a62e6a2abbef01bd99a943e554232d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cdfb4cd8853e515c6311476a9181a6e07900201dcca1f60b96d8d8bd240015ebd41f605bcadb3ef01ac05870cd4a200b96ad9de6707991810976f6e4960c71a
|
7
|
+
data.tar.gz: abe9a2419936c0b428c4b99274b1d96caaf24969038f8bbe6c2ae0e597e73de0f218b47e7d65931391b5ab21e1f789b317888f3b6b611f75185cdd712d123109
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ An opinionated read model framework for EventStore.
|
|
4
4
|
|
5
5
|
Your reducer can be anything that responds to #call.
|
6
6
|
It will receive two arguments -- the current state and the event.
|
7
|
-
The current state will be nil if no events have
|
7
|
+
The current state will be nil if no events have been processed yet.
|
8
8
|
The reducer function must return the new state.
|
9
9
|
|
10
10
|
## Routes
|
@@ -31,9 +31,8 @@ use Rack::Cors do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
use EsReadModel::
|
35
|
-
|
36
|
-
es_port: ENV['ES_PORT'],
|
34
|
+
use EsReadModel::RackSubscriber,
|
35
|
+
es_url: 'http://localhost:2113',
|
37
36
|
es_username: ENV['ES_USERNAME'],
|
38
37
|
es_password: ENV['ES_PASSWORD'],
|
39
38
|
reducer: ActiveUsers.new,
|
data/es-readmodel.gemspec
CHANGED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module EsReadModel
|
5
|
+
|
6
|
+
class RackSubscriber
|
7
|
+
|
8
|
+
attr_reader :status
|
9
|
+
|
10
|
+
def initialize(app, options)
|
11
|
+
@app = app
|
12
|
+
@listener = options[:listener]
|
13
|
+
@subscriber = Subscriber.new(options)
|
14
|
+
Thread.new { @subscriber.subscribe }
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
@request = Rack::Request.new(env)
|
19
|
+
if env['PATH_INFO'] == '/status'
|
20
|
+
status, headers, body = json_response(200, @subscriber.status)
|
21
|
+
else
|
22
|
+
env['readmodel.state'] = @subscriber.state
|
23
|
+
env['readmodel.available'] = @subscriber.status[:available]
|
24
|
+
env['readmodel.status'] = 'OK'
|
25
|
+
status, headers, body = @app.call(env)
|
26
|
+
end
|
27
|
+
@listener.call({
|
28
|
+
level: 'info',
|
29
|
+
tag: 'http.request',
|
30
|
+
msg: "#{env['REQUEST_METHOD']} #{@request.fullpath}",
|
31
|
+
status: status
|
32
|
+
})
|
33
|
+
[status, headers, body]
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def json_response(status_code, body)
|
39
|
+
result = body.merge({
|
40
|
+
_links: { self: @request.fullpath }
|
41
|
+
})
|
42
|
+
[
|
43
|
+
status_code,
|
44
|
+
{
|
45
|
+
'Content-Type' => 'application/json'
|
46
|
+
},
|
47
|
+
[result.to_json]
|
48
|
+
]
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'rack'
|
2
|
-
require 'json'
|
3
1
|
require_relative './connection'
|
4
2
|
require_relative './stream'
|
5
3
|
|
@@ -7,13 +5,12 @@ module EsReadModel
|
|
7
5
|
|
8
6
|
class Subscriber
|
9
7
|
|
10
|
-
attr_reader :status
|
8
|
+
attr_reader :status, :state
|
11
9
|
|
12
|
-
def initialize(
|
13
|
-
@app = app
|
10
|
+
def initialize(options)
|
14
11
|
@listener = options[:listener]
|
15
12
|
@initial_state = options[:initial]
|
16
|
-
url =
|
13
|
+
url = options[:es_url]
|
17
14
|
@status = {
|
18
15
|
available: false,
|
19
16
|
startedAt: Time.now,
|
@@ -26,30 +23,8 @@ module EsReadModel
|
|
26
23
|
}
|
27
24
|
@connection = Connection.new(url, options[:es_username], options[:es_password])
|
28
25
|
@reducer = options[:reducer]
|
29
|
-
Thread.new { subscribe }
|
30
26
|
end
|
31
27
|
|
32
|
-
def call(env)
|
33
|
-
@request = Rack::Request.new(env)
|
34
|
-
if env['PATH_INFO'] == '/status'
|
35
|
-
status, headers, body = json_response(200, @status)
|
36
|
-
else
|
37
|
-
env['readmodel.state'] = @state
|
38
|
-
env['readmodel.available'] = @status[:available]
|
39
|
-
env['readmodel.status'] = 'OK'
|
40
|
-
status, headers, body = @app.call(env)
|
41
|
-
end
|
42
|
-
@listener.call({
|
43
|
-
level: 'info',
|
44
|
-
tag: 'http.request',
|
45
|
-
msg: "#{env['REQUEST_METHOD']} #{@request.fullpath}",
|
46
|
-
status: status
|
47
|
-
})
|
48
|
-
[status, headers, body]
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
28
|
def subscribe
|
54
29
|
loop do
|
55
30
|
begin
|
@@ -72,6 +47,8 @@ module EsReadModel
|
|
72
47
|
end
|
73
48
|
end
|
74
49
|
|
50
|
+
private
|
51
|
+
|
75
52
|
def subscribe_to_all_events
|
76
53
|
loop do
|
77
54
|
@status[:available] = true
|
@@ -100,19 +77,6 @@ module EsReadModel
|
|
100
77
|
end
|
101
78
|
end
|
102
79
|
|
103
|
-
def json_response(status_code, body)
|
104
|
-
result = body.merge({
|
105
|
-
_links: { self: @request.fullpath }
|
106
|
-
})
|
107
|
-
[
|
108
|
-
status_code,
|
109
|
-
{
|
110
|
-
'Content-Type' => 'application/json'
|
111
|
-
},
|
112
|
-
[result.to_json]
|
113
|
-
]
|
114
|
-
end
|
115
|
-
|
116
80
|
end
|
117
81
|
|
118
82
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: es-readmodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Rutherford
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- lib/es_readmodel/event.rb
|
127
127
|
- lib/es_readmodel/logger.rb
|
128
128
|
- lib/es_readmodel/page.rb
|
129
|
+
- lib/es_readmodel/rack_subscriber.rb
|
129
130
|
- lib/es_readmodel/stream.rb
|
130
131
|
- lib/es_readmodel/subscriber.rb
|
131
132
|
homepage: https://github.com/kevinrutherford/es-readmodel
|