micro-rb 0.1.0.rc1

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.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rubocop.yml +1399 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +5 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +11 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +204 -0
  11. data/Rakefile +12 -0
  12. data/bin/console +16 -0
  13. data/bin/microrb +7 -0
  14. data/bin/setup +8 -0
  15. data/lib/micro/cli.rb +69 -0
  16. data/lib/micro/configuration.rb +34 -0
  17. data/lib/micro/examples/proto/sum.proto +12 -0
  18. data/lib/micro/examples/proto/sum_pb.rb +21 -0
  19. data/lib/micro/examples/sum.rb +22 -0
  20. data/lib/micro/handler.rb +63 -0
  21. data/lib/micro/handler_manager.rb +60 -0
  22. data/lib/micro/handlers/debug.rb +26 -0
  23. data/lib/micro/project_generator.rb +75 -0
  24. data/lib/micro/proto/debug.proto +10 -0
  25. data/lib/micro/proto/debug_pb.rb +19 -0
  26. data/lib/micro/servers/error.rb +58 -0
  27. data/lib/micro/servers/web.rb +162 -0
  28. data/lib/micro/sidecar/base.rb +26 -0
  29. data/lib/micro/sidecar/call.rb +18 -0
  30. data/lib/micro/sidecar/register.rb +18 -0
  31. data/lib/micro/version.rb +6 -0
  32. data/lib/micro-rb.rb +4 -0
  33. data/lib/microrb.rb +25 -0
  34. data/micro-rb.gemspec +37 -0
  35. data/registry.png +0 -0
  36. data/sum.png +0 -0
  37. data/templates/Dockerfile +3 -0
  38. data/templates/Gemfile +18 -0
  39. data/templates/LICENSE +21 -0
  40. data/templates/README.md +17 -0
  41. data/templates/Rakefile +9 -0
  42. data/templates/bin/app +17 -0
  43. data/templates/lib/app/handlers/example_handler.rb +16 -0
  44. data/templates/lib/app/proto/sum.proto +12 -0
  45. data/templates/lib/app/proto/sum_pb.rb +21 -0
  46. data/templates/lib/app/version.rb +4 -0
  47. data/templates/lib/app.rb +14 -0
  48. data/templates/test/app_test.rb +11 -0
  49. data/templates/test/test_helper.rb +9 -0
  50. metadata +277 -0
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ micro-rb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.4.0
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at andrew@amedeiros.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in microrb.gemspec
6
+ gemspec
7
+
8
+ group :development, :test do
9
+ gem 'awesome_print'
10
+ gem 'rubocop', '~> 0.48.1', require: false
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 amedeiros
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,204 @@
1
+ [![Build Status](https://travis-ci.org/amedeiros/micro-rb.svg?branch=master)](https://travis-ci.org/amedeiros/micro-rb)
2
+
3
+ # MicroRb
4
+
5
+ MicroRb allows you to develop micro services for the [micro](https://github.com/micro/micro) framework.
6
+ MicroRb uses the [sidecar](https://github.com/micro/micro/tree/master/car) that comes with micro. If you want to write services in Go see [go-micro](https://github.com/micro/go-micro) or in java see [ja-micro](https://github.com/Sixt/ja-micro).
7
+
8
+ ## Installation
9
+
10
+ Currently not on rubygems install with bundler and git
11
+
12
+ ```ruby
13
+ gem 'micro-rb', github: 'amedeiros/micro-rb'
14
+ ````
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'micro-rb'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install micro-rb
29
+
30
+ ## Status
31
+
32
+ Currently in development. If I can get some pull requests that would be much appreciated.
33
+
34
+ ## Google Protobufs
35
+
36
+ Currently supporting protobufs where each handler includes the module generated with requiring a Response and Request type. The example below is for the sum handler.
37
+
38
+ `$ protoc --ruby_out=. sum.proto`
39
+
40
+
41
+ ```proto
42
+ syntax = "proto3";
43
+
44
+ package micro_rb.sum_handler;
45
+
46
+ message Request {
47
+ int32 a = 1;
48
+ int32 b = 2;
49
+ }
50
+
51
+ message Response {
52
+ int32 total = 1;
53
+ }
54
+
55
+ ```
56
+
57
+ ## Usage
58
+
59
+ ```ruby
60
+ require 'microrb'
61
+ require_relative '../examples/proto/sum_pb'
62
+
63
+ class MyHandler
64
+ include MicroRb::Handler
65
+ include MicroRb::SumHandler
66
+
67
+ handler name: :test, metadata: { hello: 'Micro-Rb' }, rpc_method: :sum
68
+
69
+ def sum(request: Request.new, response: Response.new)
70
+ response.total = request.a + request.b
71
+
72
+ response
73
+ end
74
+ end
75
+
76
+ server = MicroRb::Servers::Web.new(:test, debug: true)
77
+ server.add_handler MyHandler.new
78
+ server.start!
79
+
80
+ ```
81
+
82
+ Configuration has the following defaults for sidecar endpoint.
83
+
84
+ `Host: "http://127.0.0.1"`
85
+
86
+ `Port: 8081`
87
+
88
+ `Registy: "/registry"`
89
+
90
+
91
+ Configuration can be changed.
92
+
93
+ ```ruby
94
+ MicroRb::Configuration.configure do |c|
95
+ c.sidecar_host = 'http://mysite.com'
96
+ c.sidecar_port = '8080'
97
+ c.sidecar_registry = '/awesome_registry'
98
+ end
99
+ ```
100
+
101
+ Want to run puma? No problem just add puma to your Gemfile and require the rack handler and tell the web server to use puma.
102
+ This works with thin etc because we just pass the options along to the rack server. Try it with the sum example!
103
+
104
+ ```ruby
105
+ require 'rack/handler/puma'
106
+ server = MicroRb::Servers::Web.new(:test, debug: true, server: :puma)
107
+ ```
108
+
109
+ Every handler must setup the following requirements at a minimum.
110
+ `handler name: :my_name, rpc_method: :some_method`
111
+
112
+ The `:rpc_method` must accept named parameters of `request:` and `response:`
113
+
114
+ Every handler must include a prtobuf module that has `Request` and `Response` constanst generated from protoc.
115
+
116
+
117
+ ![alt text](https://github.com/amedeiros/micro-rb/blob/master/registry.png)
118
+ ![alt text](https://github.com/amedeiros/micro-rb/blob/master/sum.png)
119
+
120
+
121
+ ## Micro API Gateway
122
+
123
+ `micro api --address 0.0.0.0:3002`
124
+
125
+ ```
126
+ $ http POST 0.0.0.0:3002/rpc method=MyHandler.sum service=test request='{"a": 1, "b": 2}'
127
+ HTTP/1.1 200 OK
128
+ Access-Control-Allow-Credentials: true
129
+ Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization
130
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
131
+ Content-Length: 11
132
+ Content-Type: application/json
133
+ Date: Tue, 09 May 2017 17:50:47 GMT
134
+
135
+ {
136
+ "total": 3
137
+ }
138
+ ```
139
+
140
+ ## Calling the service directly
141
+
142
+ ```
143
+ $ http POST 0.0.0.0:3000 service=test method=MyHandler.sum id=1 params:='[{"a": 1, "b": 2}]'
144
+ HTTP/1.1 200 OK
145
+ Connection: Keep-Alive
146
+ Content-Length: 31
147
+ Date: Tue, 09 May 2017 18:08:25 GMT
148
+ Server: WEBrick/1.3.1 (Ruby/2.4.0/2016-12-24)
149
+
150
+ {"result":{"total":3},"id":"1"}
151
+ ```
152
+
153
+ ## Project Generator
154
+
155
+ ```
156
+ microrb <options>
157
+ -n, --new NAME Generate a new skeleton service.
158
+ -e, --encryption Adds Symmetric Encryption gem to your new service.
159
+ -a, --activerecord Adds ActiveRecord to your gemfile and a default DB setup.
160
+ -h, --help Display this help screen
161
+ ```
162
+
163
+
164
+ To generate a new micro service project run the following. Note this also adds [Symmetric Encryption](https://github.com/rocketjob/symmetric-encryption) gem with the -e flag.
165
+
166
+ `microrb -n myservice -e`
167
+
168
+ This will output a new project with the example sum service ready to run.
169
+
170
+ ```
171
+ Generating new service called myservice...
172
+ Fetching gem metadata from https://rubygems.org/.............
173
+ Fetching version metadata from https://rubygems.org/.
174
+ Resolving dependencies...
175
+ ...... More bundler stuff here
176
+ Complete...
177
+ Please see https://rocketjob.github.io/symmetric-encryption/standalone.html for setting up SymmetricEncryption
178
+ Run sidecar: micro sidecar
179
+ Run micro web: micro --web_address 0.0.0.0:8080 web
180
+ Run me: ./myservice/bin/myservice
181
+ ```
182
+
183
+ ```
184
+ ./myservice/bin/myservice
185
+ [2017-05-05 14:28:52] INFO WEBrick 1.3.1
186
+ [2017-05-05 14:28:52] INFO ruby 2.3.1 (2017-03-06) [java]
187
+ [2017-05-05 14:28:52] INFO WEBrick::HTTPServer#start: pid=48485 port=3000
188
+ ```
189
+
190
+ ## Development
191
+
192
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
193
+
194
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
195
+
196
+ ## Contributing
197
+
198
+ Bug reports and pull requests are welcome on GitHub at https://github.com/amedeiros/micro-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
199
+
200
+
201
+ ## License
202
+
203
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
204
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'bundler/setup'
6
+ require 'microrb'
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require 'irb'
16
+ IRB.start(__FILE__)
data/bin/microrb ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ # frozen_string_literal: true
4
+
5
+ require 'micro/cli'
6
+
7
+ MicroRb::CLI.new(ARGV).run!
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/micro/cli.rb ADDED
@@ -0,0 +1,69 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'optparse'
5
+ require 'micro/project_generator'
6
+
7
+ module MicroRb
8
+ class CLI
9
+ attr_reader :parser, :options
10
+
11
+ def initialize(argv)
12
+ setup
13
+ @options = {}
14
+ parser.parse! argv.dup
15
+ end
16
+
17
+ def run!
18
+ if options.empty?
19
+ puts parser
20
+ exit
21
+ end
22
+
23
+ generate_new if options.key?(:new)
24
+ end
25
+
26
+ private
27
+
28
+ def setup
29
+ @parser = OptionParser.new do |opts|
30
+ opts.banner = 'microrb <options>'
31
+
32
+ opts.on '-n', '--new NAME', 'Generate a new skeleton service.' do |name|
33
+ options[:new] = name
34
+ end
35
+
36
+ opts.on '-e', '--encryption', 'Adds Symmetric Encryption gem to your new service.' do |encryption|
37
+ options[:encryption] = encryption
38
+ end
39
+
40
+ opts.on '-a', '--activerecord', 'Adds ActiveRecord to your gemfile and a default DB setup.' do |ar|
41
+ options[:active_record] = ar
42
+ end
43
+
44
+ opts.on '-h', '--help', 'Display this help screen' do
45
+ puts opts
46
+ exit
47
+ end
48
+ end
49
+ end
50
+
51
+ def generate_new
52
+ puts "Generating new service called #{options[:new]}..."
53
+ active_record = options[:active_record]
54
+ encryption = options[:encryption]
55
+ name = options[:new]
56
+
57
+ ProjectGenerator.new(name, encryption, active_record).create!
58
+
59
+ puts 'Complete...'
60
+ if options[:encryption]
61
+ puts 'Please see https://rocketjob.github.io/symmetric-encryption/standalone.html'\
62
+ ' for setting up SymmetricEncryption'
63
+ end
64
+ puts 'Run sidecar: micro sidecar'
65
+ puts 'Run micro web: micro --web_address 0.0.0.0:8080 web'
66
+ puts "Run me: ./#{name}/bin/#{name}"
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module MicroRb
5
+ class Configuration
6
+ include Singleton
7
+
8
+ attr_accessor :sidecar_registry
9
+ attr_accessor :sidecar_host
10
+ attr_accessor :sidecar_port
11
+
12
+ def self.configure
13
+ yield(instance) if block_given?
14
+ end
15
+
16
+ def sidecar_uri
17
+ "#{sidecar_host}:#{sidecar_port}"
18
+ end
19
+
20
+ def sidecar_registry_uri
21
+ "#{sidecar_uri}#{sidecar_registry}"
22
+ end
23
+
24
+ private
25
+
26
+ def initialize
27
+ yield(self) if block_given?
28
+
29
+ self.sidecar_host ||= 'http://127.0.0.1'
30
+ self.sidecar_registry ||= '/registry'
31
+ self.sidecar_port ||= '8081'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ syntax = "proto3";
2
+
3
+ package micro_rb.sum_handler;
4
+
5
+ message Request {
6
+ int32 a = 1;
7
+ int32 b = 2;
8
+ }
9
+
10
+ message Response {
11
+ int32 total = 1;
12
+ }
@@ -0,0 +1,21 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # source: sum.proto
3
+
4
+ require 'google/protobuf'
5
+
6
+ Google::Protobuf::DescriptorPool.generated_pool.build do
7
+ add_message 'micro_rb.sum_handler.Request' do
8
+ optional :a, :int32, 1
9
+ optional :b, :int32, 2
10
+ end
11
+ add_message 'micro_rb.sum_handler.Response' do
12
+ optional :total, :int32, 1
13
+ end
14
+ end
15
+
16
+ module MicroRb
17
+ module SumHandler
18
+ Request = Google::Protobuf::DescriptorPool.generated_pool.lookup('micro_rb.sum_handler.Request').msgclass
19
+ Response = Google::Protobuf::DescriptorPool.generated_pool.lookup('micro_rb.sum_handler.Response').msgclass
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../../../lib/microrb'
5
+ require_relative '../examples/proto/sum_pb'
6
+
7
+ class MyHandler
8
+ include MicroRb::Handler
9
+ include MicroRb::SumHandler
10
+
11
+ handler name: :test, metadata: { hello: 'Micro-Rb' }, rpc_method: :sum
12
+
13
+ def sum(request: Request.new, response: Response.new)
14
+ response.total = request.a + request.b
15
+
16
+ response
17
+ end
18
+ end
19
+
20
+ server = MicroRb::Servers::Web.new(:test, debug: true, metadata: { example: 'Service' })
21
+ server.add_handler MyHandler.new
22
+ server.start!
@@ -0,0 +1,63 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module MicroRb
5
+ module Handler
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ include SemanticLogger::Loggable
10
+
11
+ def self.handler(options)
12
+ raise 'Missing name' unless options.key?(:name)
13
+ raise 'Missing rpc_method' unless options.key?(:rpc_method)
14
+ raise 'Metadata should be a Hash' if options.key?(:metadata) && !options.is_a?(Hash)
15
+
16
+ class_attribute :name
17
+ public_send('name=', options[:name])
18
+
19
+ class_attribute :metadata
20
+ public_send('metadata=', options[:metadata] || {})
21
+
22
+ class_attribute :rpc_method
23
+ public_send('rpc_method=', options[:rpc_method].to_sym)
24
+ end
25
+
26
+ def full_rpc_name
27
+ "#{self.class.to_s.split('::').last}.#{rpc_method}"
28
+ end
29
+
30
+ def request_structure
31
+ build_structure(self.class::Request)
32
+ end
33
+
34
+ def response_structure
35
+ build_structure(self.class::Response)
36
+ end
37
+
38
+ def endpoint_structure
39
+ { name: full_rpc_name, request: request_structure,
40
+ response: response_structure, metadata: metadata }
41
+ end
42
+
43
+ def valid?
44
+ self.class.constants.include?(:Request) &&
45
+ self.class.constants.include?(:Response) &&
46
+ name.present? &&
47
+ rpc_method.present?
48
+ end
49
+
50
+ private
51
+
52
+ def build_structure(type)
53
+ structure_values = []
54
+
55
+ type.descriptor.entries.each do |descriptor|
56
+ structure_values << { name: descriptor.name, type: descriptor.type }
57
+ end
58
+
59
+ { values: structure_values }
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,60 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module MicroRb
5
+ class HandlerManager
6
+ attr_reader :handlers, :rpc_methods, :endpoints
7
+
8
+ def initialize
9
+ @handlers = []
10
+ @rpc_methods = {}
11
+ @endpoints = []
12
+ end
13
+
14
+ def add_handler(handler)
15
+ validate_handler(handler)
16
+ add_rpc_method(handler)
17
+ add_endpoints(handler)
18
+
19
+ handlers << handler.name
20
+ end
21
+
22
+ def rpc_method(method)
23
+ rpc_methods[method.to_sym]
24
+ end
25
+
26
+ def rpc_method?(method)
27
+ rpc_methods.key?(method.to_sym)
28
+ end
29
+
30
+ def rpc_method_response(method)
31
+ rpc_method(method).owner::Response.new
32
+ end
33
+
34
+ def rpc_method_request(method, params)
35
+ rpc_method(method).owner::Request.new(*params)
36
+ end
37
+
38
+ private
39
+
40
+ def validate_handler(handler)
41
+ unless handler.is_a?(MicroRb::Handler)
42
+ raise "Handler must be of type MicroRb::Handler got #{handler.class}"
43
+ end
44
+
45
+ if handlers.include?(handler.name)
46
+ raise "Handler #{handler.name} has already been registered."
47
+ end
48
+
49
+ raise "Handler #{handler.name} is invalid." unless handler.valid?
50
+ end
51
+
52
+ def add_rpc_method(handler)
53
+ rpc_methods[handler.full_rpc_name.to_sym] = handler.method(handler.rpc_method)
54
+ end
55
+
56
+ def add_endpoints(handler)
57
+ @endpoints << handler.endpoint_structure
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'micro/proto/debug_pb'
5
+
6
+ module MicroRb
7
+ module Handlers
8
+ class Debug
9
+ include MicroRb::Handler
10
+ include MicroRb::Debug
11
+
12
+ handler name: :debug, metadata: { about: 'Health check endpoint' }, rpc_method: :health
13
+
14
+ def health(request: Request.new, response: Response.new)
15
+ response.status = 'ok' # default
16
+
17
+ response
18
+ end
19
+
20
+ # Override this to set Health capital.
21
+ def full_rpc_name
22
+ "#{self.class.to_s.split('::').last}.Health"
23
+ end
24
+ end
25
+ end
26
+ end