barrister-amqp 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +61 -0
- data/Rakefile +2 -0
- data/barrister-amqp.gemspec +27 -0
- data/lib/barrister/amqp/version.rb +5 -0
- data/lib/barrister/amqp.rb +71 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a9c06cedf7afd42e8b3f300be62da1abe0c3adff
|
4
|
+
data.tar.gz: 4d4c8fd93a937b7d9eea6f3a7b0b83049077467a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 08986cbb2aaf996b1051560ec31187d1907091cf85bcc9f1fd0d1d48caaf3899e0bd585bd9fdbafd7d152be6b41d877db175a57fca15e386ddb8707a03fb6e80
|
7
|
+
data.tar.gz: cba7f4b7705efb61530e0385a342c3d95d854f108ff05072f927e5119581f2ae5406137aa459bd09f44dd846cbe25a3627be2dbb2c0f28a2135a840de3bae78d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 gregory
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Barrister::Amqp
|
2
|
+
|
3
|
+
A Amqp server-container and transport for [Barrister RPC](http://barrister.bitmechanic.com)
|
4
|
+
|
5
|
+
Feel free to go read about the concepts at [http://barrister.bitmechanic.com](http://barrister.bitmechanic.com)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'barrister-amqp'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install barrister-amqp
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Since we are using AMQP, you'll have to setup an `AMQP_URL` in your environment.
|
26
|
+
|
27
|
+
On your service side, you'll wrap your Barrister server in the AMQP container like this:
|
28
|
+
|
29
|
+
```rb
|
30
|
+
ENV['AMQP_URL']="amqp://user:password@hostname/vhost"
|
31
|
+
server = Barrister::Amqp::Container.new('full_path_to_your_service.json', 'com.company.services.my_barrister_service')
|
32
|
+
server.add_handler('ServiceA', ServiceA)
|
33
|
+
server.start
|
34
|
+
```
|
35
|
+
|
36
|
+
Calling `start`on the Amqp container will connect to your AMQP server and begin to
|
37
|
+
listen for messages
|
38
|
+
|
39
|
+
On your client side, you'll wrap the transport layer of your client like this:
|
40
|
+
|
41
|
+
```rb
|
42
|
+
ENV['AMQP_URL']="amqp://user:password@hostname/vhost"
|
43
|
+
amqp_transport = Barrister::Amqp::Transport.new('com.company.services.my_barrister_service')
|
44
|
+
client = Barrister::Client.new(amqp_transport)
|
45
|
+
|
46
|
+
client.ServiceA.my_awesome_rpc_method
|
47
|
+
```
|
48
|
+
|
49
|
+
## TODO
|
50
|
+
|
51
|
+
- [ ] write specs
|
52
|
+
- [ ] write example
|
53
|
+
- [ ] see if we can make it more robust like gocardless did with [hutch](https://github.com/gocardless/hutch/tree/master/lib/hutch/error_handlers)
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
1. Fork it ( https://github.com/[my-github-username]/barrister-amqp/fork )
|
58
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
59
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
60
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
61
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'barrister/amqp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "barrister-amqp"
|
8
|
+
spec.version = Barrister::Amqp::VERSION
|
9
|
+
spec.authors = ["gregory"]
|
10
|
+
spec.email = ["greg2502@gmail.com"]
|
11
|
+
spec.summary = %q{AMQP transport and server-container for Barrister}
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "bunny", "~> 2.2.2"
|
22
|
+
spec.add_dependency 'barrister', '~> 0'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "pry", "> 0"
|
27
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "barrister/amqp/version"
|
2
|
+
require 'bunny'
|
3
|
+
|
4
|
+
module Barrister
|
5
|
+
module Amqp
|
6
|
+
class Transport
|
7
|
+
# NOTE: transport needs to implement request method for the Barrister::Client to
|
8
|
+
# send requests to the server
|
9
|
+
def initialize(service_name, options={})
|
10
|
+
conn = Bunny.new ENV.fetch('AMQP_URL') #"Please set AMQP_URL to something like: amqp://user:password@host:port/vhost"
|
11
|
+
conn.start
|
12
|
+
@ch = conn.create_channel
|
13
|
+
@service_q = @ch.queue(service_name, auto_delete: false)
|
14
|
+
@reply_q = @ch.queue('', exclusive: true)
|
15
|
+
@x = @ch.default_exchange
|
16
|
+
@response_table = Hash.new { |h,k| h[k] = Queue.new }
|
17
|
+
|
18
|
+
@reply_q.subscribe(block: false) do |delivery_info, properties, payload|
|
19
|
+
@response_table[properties[:correlation_id]].push payload # push anything that comes in the response_q
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def request(message)
|
24
|
+
@x.publish(JSON.generate(message, { :ascii_only=>true }), { correlation_id: message['id'], reply_to: @reply_q.name, routing_key: @service_q.name})
|
25
|
+
|
26
|
+
response = @response_table[message['id']].pop
|
27
|
+
@response_table.delete message['id']
|
28
|
+
|
29
|
+
begin
|
30
|
+
puts "got response: #{JSON.parse(response)}"
|
31
|
+
JSON.parse(response)
|
32
|
+
rescue JSON::ParserError => e
|
33
|
+
raise RpcException.new(-32000, "Bad response #{e.message}")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Container
|
39
|
+
# NOTE: A container needs to call the Barrister::Server to handle the request
|
40
|
+
# and send the response to the client.
|
41
|
+
#
|
42
|
+
def initialize(json_path, service_name, handlers=[], options={})
|
43
|
+
conn = Bunny.new ENV.fetch('AMQP_URL')
|
44
|
+
conn.start
|
45
|
+
@ch = conn.create_channel
|
46
|
+
@service_q = @ch.queue(service_name, auto_delete: false)
|
47
|
+
@x = @ch.default_exchange
|
48
|
+
|
49
|
+
contract = Barrister::contract_from_file(json_path)
|
50
|
+
@server = Barrister::Server.new(contract)
|
51
|
+
|
52
|
+
Array(handlers).each do |handler|
|
53
|
+
iface_name = handler.class.to_s.split('::').last
|
54
|
+
@server.add_handler iface_name, handler
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# TODO: use delegator
|
59
|
+
def add_handler(iface_name, handler)
|
60
|
+
@server.add_handler iface_name, handler
|
61
|
+
end
|
62
|
+
|
63
|
+
def start
|
64
|
+
@service_q.subscribe(block: true) do |delivery_info, properties, payload|
|
65
|
+
puts "handle #{payload}"
|
66
|
+
@x.publish(@server.handle_json(payload), { routing_key: properties.reply_to, correlation_id: properties.correlation_id })
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: barrister-amqp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- gregory
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bunny
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.2.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.2.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: barrister
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: AMQP transport and server-container for Barrister
|
84
|
+
email:
|
85
|
+
- greg2502@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- barrister-amqp.gemspec
|
96
|
+
- lib/barrister/amqp.rb
|
97
|
+
- lib/barrister/amqp/version.rb
|
98
|
+
homepage: ''
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
metadata: {}
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 2.2.2
|
119
|
+
signing_key:
|
120
|
+
specification_version: 4
|
121
|
+
summary: AMQP transport and server-container for Barrister
|
122
|
+
test_files: []
|