jetra 1.0.4 → 1.1.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 +5 -5
- data/lib/jetra/adapter/grpc.rb +94 -0
- data/lib/jetra/adapter/grpc/jetra_pb.rb +24 -0
- data/lib/jetra/adapter/grpc/jetra_services_pb.rb +24 -0
- data/lib/jetra/adapter/rack.rb +1 -0
- data/lib/jetra/adapter/thrift.rb +46 -1
- data/lib/jetra/base.rb +45 -49
- data/lib/jetra/middleware/validater.rb +3 -3
- data/lib/jetra/version.rb +1 -1
- metadata +6 -4
- data/lib/jetra/client/thrift.rb +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1841c50b2435c1f93d9282553d527b31d6799ed2a90701b4c3dad2cf614e1aa0
|
4
|
+
data.tar.gz: dbafef64109519c9ea4adf70dbea208349ab18ba2855b346fdaedfb16eb33fd3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d6af2c6ed57fd5aa5270a807fdfc720335a4b717873ea396f1fcea7a58ec802be8f4cffa40811a955a078563033d6e0b449760fdfb8cd96241dd2d585c0e0e4
|
7
|
+
data.tar.gz: 519e259d4550d4523b1373c455637489205c5e570210f0d38b9b1889a3372eaf4bd76f47ec356361fce70808cf485058b192e6c1c83e22934358d4b462c60e61
|
@@ -0,0 +1,94 @@
|
|
1
|
+
|
2
|
+
require 'grpc'
|
3
|
+
|
4
|
+
require "jetra/adapter/grpc/jetra_pb"
|
5
|
+
require "jetra/adapter/grpc/jetra_services_pb"
|
6
|
+
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
module Jetra
|
10
|
+
|
11
|
+
class GrpcAdapter < Jetra::Grpc::Interface::Service
|
12
|
+
|
13
|
+
def initialize(app, &custom_block)
|
14
|
+
|
15
|
+
@app = app
|
16
|
+
|
17
|
+
@custom_block = custom_block
|
18
|
+
end
|
19
|
+
|
20
|
+
def call(request, _unused_call)
|
21
|
+
|
22
|
+
if @custom_block
|
23
|
+
@custom_block.call(request)
|
24
|
+
end
|
25
|
+
|
26
|
+
route = request.route || ""
|
27
|
+
|
28
|
+
params = parse_params(request.params)
|
29
|
+
|
30
|
+
sym_route = route.to_sym
|
31
|
+
|
32
|
+
res = @app.call(sym_route, params)
|
33
|
+
|
34
|
+
response = Jetra::Grpc::JetraResponse.new
|
35
|
+
response.status = res.status
|
36
|
+
response.body = res.body.to_json
|
37
|
+
response
|
38
|
+
end
|
39
|
+
|
40
|
+
def parse_params(params)
|
41
|
+
indifferent_params(JSON.load(params).to_h)
|
42
|
+
rescue => boom
|
43
|
+
{}
|
44
|
+
end
|
45
|
+
|
46
|
+
# Enable string or symbol key access to the nested params hash.
|
47
|
+
def indifferent_params(object)
|
48
|
+
case object
|
49
|
+
when Hash
|
50
|
+
new_hash = indifferent_hash
|
51
|
+
object.each { |key, value| new_hash[key] = indifferent_params(value) }
|
52
|
+
new_hash
|
53
|
+
when Array
|
54
|
+
object.map { |item| indifferent_params(item) }
|
55
|
+
else
|
56
|
+
object
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Creates a Hash with indifferent access.
|
61
|
+
def indifferent_hash
|
62
|
+
Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class GrpcServer
|
67
|
+
|
68
|
+
def initialize(handler, bind)
|
69
|
+
|
70
|
+
server = GRPC::RpcServer.new
|
71
|
+
server.add_http2_port(bind, :this_port_is_insecure)
|
72
|
+
server.handle(handler)
|
73
|
+
|
74
|
+
@server = server
|
75
|
+
end
|
76
|
+
|
77
|
+
def serve
|
78
|
+
@server.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class GrpcClient
|
83
|
+
|
84
|
+
def initialize(host_and_port)
|
85
|
+
@client = Jetra::Grpc::Interface::Stub.new(host_and_port, :this_channel_is_insecure)
|
86
|
+
end
|
87
|
+
|
88
|
+
def call(request)
|
89
|
+
@client.call(request)
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
2
|
+
# source: jetra.proto
|
3
|
+
|
4
|
+
require 'google/protobuf'
|
5
|
+
|
6
|
+
Google::Protobuf::DescriptorPool.generated_pool.build do
|
7
|
+
add_file("jetra.proto", :syntax => :proto3) do
|
8
|
+
add_message "jetra.grpc.JetraRequest" do
|
9
|
+
optional :route, :string, 1
|
10
|
+
optional :params, :string, 2
|
11
|
+
end
|
12
|
+
add_message "jetra.grpc.JetraResponse" do
|
13
|
+
optional :status, :int32, 1
|
14
|
+
optional :body, :string, 2
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Jetra
|
20
|
+
module Grpc
|
21
|
+
JetraRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("jetra.grpc.JetraRequest").msgclass
|
22
|
+
JetraResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("jetra.grpc.JetraResponse").msgclass
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
2
|
+
# Source: jetra.proto for package 'jetra.grpc'
|
3
|
+
|
4
|
+
require 'grpc'
|
5
|
+
require_relative 'jetra_pb'
|
6
|
+
|
7
|
+
module Jetra
|
8
|
+
module Grpc
|
9
|
+
module Interface
|
10
|
+
class Service
|
11
|
+
|
12
|
+
include GRPC::GenericService
|
13
|
+
|
14
|
+
self.marshal_class_method = :encode
|
15
|
+
self.unmarshal_class_method = :decode
|
16
|
+
self.service_name = 'jetra.grpc.Interface'
|
17
|
+
|
18
|
+
rpc :Call, JetraRequest, JetraResponse
|
19
|
+
end
|
20
|
+
|
21
|
+
Stub = Service.rpc_stub_class
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/jetra/adapter/rack.rb
CHANGED
data/lib/jetra/adapter/thrift.rb
CHANGED
@@ -5,7 +5,6 @@ require "jetra/adapter/thrift/jetra_types"
|
|
5
5
|
require "jetra/adapter/thrift/jetra_constants"
|
6
6
|
require "jetra/adapter/thrift/service"
|
7
7
|
|
8
|
-
require "set"
|
9
8
|
require 'json'
|
10
9
|
|
11
10
|
module Jetra
|
@@ -66,4 +65,50 @@ module Jetra
|
|
66
65
|
|
67
66
|
end
|
68
67
|
|
68
|
+
class ThriftServer
|
69
|
+
|
70
|
+
def initialize(handler, port)
|
71
|
+
port = port
|
72
|
+
processor = Thrift::Service::Processor.new(handler)
|
73
|
+
transport = ::Thrift::ServerSocket.new(port)
|
74
|
+
transportFactory = ::Thrift::FramedTransportFactory.new()
|
75
|
+
@server = ::Thrift::NonblockingServer.new(processor, transport, transportFactory)
|
76
|
+
end
|
77
|
+
|
78
|
+
def serve
|
79
|
+
@server.serve()
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
class ThriftClient
|
85
|
+
|
86
|
+
def initialize(host, port)
|
87
|
+
|
88
|
+
transport = ::Thrift::FramedTransport.new(::Thrift::Socket.new(host, port))
|
89
|
+
protocol = ::Thrift::BinaryProtocol.new(transport)
|
90
|
+
|
91
|
+
@transport = transport
|
92
|
+
|
93
|
+
@client = Thrift::Service::Client.new(protocol)
|
94
|
+
end
|
95
|
+
|
96
|
+
def call(request)
|
97
|
+
|
98
|
+
@client.call(request)
|
99
|
+
|
100
|
+
rescue ::Thrift::TransportException, IOError => boom
|
101
|
+
|
102
|
+
establish_connection
|
103
|
+
|
104
|
+
@client.call(request)
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
def establish_connection
|
109
|
+
@transport.open
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
69
114
|
end
|
data/lib/jetra/base.rb
CHANGED
@@ -4,7 +4,7 @@ module Jetra
|
|
4
4
|
|
5
5
|
class NotFoundException < Exception ; end
|
6
6
|
|
7
|
-
class Halt ; end
|
7
|
+
class Halt < Exception ; end
|
8
8
|
|
9
9
|
class Request
|
10
10
|
|
@@ -20,7 +20,7 @@ module Jetra
|
|
20
20
|
|
21
21
|
attr_accessor :status, :body
|
22
22
|
|
23
|
-
def initialize(status
|
23
|
+
def initialize(status=-1, body="")
|
24
24
|
@status = status.to_i
|
25
25
|
@body = body
|
26
26
|
end
|
@@ -43,31 +43,31 @@ module Jetra
|
|
43
43
|
@request = Request.new(route, params)
|
44
44
|
@response = Response.new
|
45
45
|
|
46
|
-
@params =
|
46
|
+
@params = indifferent_params(@request.params)
|
47
47
|
|
48
48
|
invoke { dispatch! }
|
49
49
|
|
50
50
|
@response.finish
|
51
51
|
end
|
52
52
|
|
53
|
-
def
|
53
|
+
def current_class
|
54
54
|
self.class
|
55
55
|
end
|
56
56
|
|
57
|
-
def
|
57
|
+
def indifferent_params(object)
|
58
58
|
case object
|
59
59
|
when Hash
|
60
|
-
newHash =
|
61
|
-
object.each { |key, value| newHash[key] =
|
60
|
+
newHash = indifferent_hash
|
61
|
+
object.each { |key, value| newHash[key] = indifferent_params(value) }
|
62
62
|
newHash
|
63
63
|
when Array
|
64
|
-
object.map { |item|
|
64
|
+
object.map { |item| indifferent_params(item) }
|
65
65
|
else
|
66
66
|
object
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
-
def
|
70
|
+
def indifferent_hash
|
71
71
|
Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
|
72
72
|
end
|
73
73
|
|
@@ -97,20 +97,20 @@ module Jetra
|
|
97
97
|
route!
|
98
98
|
rescue ::Exception => boom
|
99
99
|
gotError = true
|
100
|
-
|
100
|
+
handle_exception!(boom)
|
101
101
|
ensure
|
102
102
|
begin
|
103
103
|
filter! :after
|
104
104
|
rescue ::Exception => boom
|
105
|
-
|
105
|
+
handle_exception!(boom) unless gotError
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
|
-
def
|
109
|
+
def handle_exception!(boom)
|
110
110
|
|
111
|
-
response.status =
|
111
|
+
response.status = -1
|
112
112
|
|
113
|
-
|
113
|
+
error_block!(boom.class, boom)
|
114
114
|
|
115
115
|
raise boom
|
116
116
|
end
|
@@ -121,25 +121,25 @@ module Jetra
|
|
121
121
|
end
|
122
122
|
|
123
123
|
def filter!(type)
|
124
|
-
|
124
|
+
current_class.filters[type].each do |args|
|
125
125
|
processRoute(*args)
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
129
129
|
def route!
|
130
130
|
|
131
|
-
if block =
|
131
|
+
if block = current_class.routes[@request.route.to_sym]
|
132
132
|
processRoute do |*args|
|
133
133
|
routeEval { block[*args] }
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
137
|
-
|
137
|
+
route_missing
|
138
138
|
end
|
139
139
|
|
140
|
-
def
|
140
|
+
def error_block!(errorClass, *blockParams)
|
141
141
|
|
142
|
-
if errorBlocks =
|
142
|
+
if errorBlocks = current_class.errors[errorClass]
|
143
143
|
errorBlocks.reverse_each do |errorBlock|
|
144
144
|
args = [errorBlock]
|
145
145
|
args += [blockParams]
|
@@ -148,7 +148,7 @@ module Jetra
|
|
148
148
|
end
|
149
149
|
|
150
150
|
if errorClass.respond_to? :superclass and errorClass.superclass <= Exception
|
151
|
-
|
151
|
+
error_block!(errorClass.superclass, *blockParams)
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
@@ -160,42 +160,34 @@ module Jetra
|
|
160
160
|
block ? block[self,values] : yield(self,values)
|
161
161
|
end
|
162
162
|
|
163
|
-
def
|
163
|
+
def route_missing
|
164
164
|
raise NotFoundException.new("route not found")
|
165
165
|
end
|
166
166
|
|
167
|
-
def
|
168
|
-
status = args[:status]
|
169
|
-
raise "status code must >= 1 when using success" if status < 1
|
167
|
+
def success_response(body, status=0)
|
170
168
|
|
171
|
-
if
|
172
|
-
body = {msg: body}
|
173
|
-
end
|
169
|
+
raise "status code must >= 0 when success" if status < 0
|
174
170
|
|
175
171
|
response.body = body
|
176
172
|
response.status = status
|
177
|
-
nil
|
178
173
|
end
|
179
174
|
|
180
|
-
def
|
181
|
-
status = args[:status]
|
182
|
-
raise "status code must <= -1 when using failure" if status > -1
|
175
|
+
def failure_response(body, status=-1)
|
183
176
|
|
184
|
-
if
|
185
|
-
body = {msg: body}
|
186
|
-
end
|
177
|
+
raise "status code must <= -1 when failure" if status > -1
|
187
178
|
|
188
179
|
response.body = body
|
189
180
|
response.status = status
|
190
|
-
nil
|
191
181
|
end
|
192
182
|
|
193
|
-
def
|
194
|
-
|
183
|
+
def halt_success(body, status=0)
|
184
|
+
success_response(body, status)
|
185
|
+
halt
|
195
186
|
end
|
196
187
|
|
197
|
-
def
|
198
|
-
|
188
|
+
def halt_failure(body, status=-1)
|
189
|
+
failure_response(body, status)
|
190
|
+
halt
|
199
191
|
end
|
200
192
|
|
201
193
|
class << self
|
@@ -211,14 +203,14 @@ module Jetra
|
|
211
203
|
end
|
212
204
|
|
213
205
|
def before(&block)
|
214
|
-
|
206
|
+
add_filter(:before, &block)
|
215
207
|
end
|
216
208
|
|
217
209
|
def after(&block)
|
218
|
-
|
210
|
+
add_filter(:after, &block)
|
219
211
|
end
|
220
212
|
|
221
|
-
def
|
213
|
+
def add_filter(type, &block)
|
222
214
|
@filters[type] << compile!(&block)
|
223
215
|
end
|
224
216
|
|
@@ -252,14 +244,14 @@ module Jetra
|
|
252
244
|
|
253
245
|
def inherited(subclass)
|
254
246
|
|
255
|
-
subclass.routes =
|
256
|
-
subclass.filters =
|
257
|
-
subclass.errors =
|
247
|
+
subclass.routes = copy_routes
|
248
|
+
subclass.filters = copy_filters
|
249
|
+
subclass.errors = copy_errors
|
258
250
|
|
259
251
|
super
|
260
252
|
end
|
261
253
|
|
262
|
-
def
|
254
|
+
def copy_routes
|
263
255
|
newRoutes = {}
|
264
256
|
@routes.each do |key, value|
|
265
257
|
newRoutes[key] = value
|
@@ -267,7 +259,7 @@ module Jetra
|
|
267
259
|
newRoutes
|
268
260
|
end
|
269
261
|
|
270
|
-
def
|
262
|
+
def copy_filters
|
271
263
|
newFilters = {}
|
272
264
|
@filters.each do |key, values|
|
273
265
|
newValues = []
|
@@ -279,7 +271,7 @@ module Jetra
|
|
279
271
|
newFilters
|
280
272
|
end
|
281
273
|
|
282
|
-
def
|
274
|
+
def copy_errors
|
283
275
|
newErrors = {}
|
284
276
|
@errors.each do |key, values|
|
285
277
|
newValues = []
|
@@ -310,12 +302,16 @@ module Jetra
|
|
310
302
|
@errors = {}
|
311
303
|
|
312
304
|
error do |boom|
|
305
|
+
|
306
|
+
boommsg = "#{boom.class} - #{boom.message}"
|
307
|
+
|
313
308
|
if boom.class == Jetra::NotFoundException
|
314
309
|
trace = []
|
315
310
|
else
|
316
311
|
trace = boom.backtrace
|
312
|
+
trace.unshift boommsg
|
317
313
|
end
|
318
|
-
response.body = {msg:
|
314
|
+
response.body = {msg: boommsg, class: boom.class.to_s, route: request.route, params: params, trace: trace}
|
319
315
|
halt
|
320
316
|
end
|
321
317
|
|
@@ -13,16 +13,16 @@ module Jetra
|
|
13
13
|
|
14
14
|
if !params.kind_of?(Hash)
|
15
15
|
response = Jetra::Response.new
|
16
|
-
response.status =
|
16
|
+
response.status = -1
|
17
17
|
response.body = {msg: "Jetra::Middleware::Validater: params type miss match. excepted Hash, got #{params.class.to_s}"}
|
18
18
|
else
|
19
19
|
response = @app.call(route, params)
|
20
20
|
if !response.status.kind_of?(Integer)
|
21
|
-
response.status =
|
21
|
+
response.status = -1
|
22
22
|
response.body = {msg: "Jetra::Middleware::Validater: response.status type miss match. excepted Integer, got #{response.status.class.to_s}"}
|
23
23
|
else
|
24
24
|
if !response.body.kind_of?(Hash)
|
25
|
-
response.status =
|
25
|
+
response.status = -1
|
26
26
|
response.body = {msg: "Jetra::Middleware::Validater: response.body type miss match. excepted Hash, got #{response.body.class.to_s}"}
|
27
27
|
end
|
28
28
|
end
|
data/lib/jetra/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jetra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeffrey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: micro DSL
|
14
14
|
email:
|
@@ -18,6 +18,9 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/jetra.rb
|
21
|
+
- lib/jetra/adapter/grpc.rb
|
22
|
+
- lib/jetra/adapter/grpc/jetra_pb.rb
|
23
|
+
- lib/jetra/adapter/grpc/jetra_services_pb.rb
|
21
24
|
- lib/jetra/adapter/rack.rb
|
22
25
|
- lib/jetra/adapter/thrift.rb
|
23
26
|
- lib/jetra/adapter/thrift/jetra_constants.rb
|
@@ -26,7 +29,6 @@ files:
|
|
26
29
|
- lib/jetra/application.rb
|
27
30
|
- lib/jetra/base.rb
|
28
31
|
- lib/jetra/builder.rb
|
29
|
-
- lib/jetra/client/thrift.rb
|
30
32
|
- lib/jetra/combiner.rb
|
31
33
|
- lib/jetra/middleware/sample.rb
|
32
34
|
- lib/jetra/middleware/validater.rb
|
@@ -51,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
53
|
version: '0'
|
52
54
|
requirements: []
|
53
55
|
rubyforge_project:
|
54
|
-
rubygems_version: 2.
|
56
|
+
rubygems_version: 2.7.8
|
55
57
|
signing_key:
|
56
58
|
specification_version: 4
|
57
59
|
summary: make it easy to write micro service
|
data/lib/jetra/client/thrift.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
#用于封装基于jetra的thrift接口客户端,方便使用方调用
|
2
|
-
|
3
|
-
module Jetra
|
4
|
-
|
5
|
-
class ThriftClient
|
6
|
-
|
7
|
-
def initialize(thriftService, availableMethods)
|
8
|
-
|
9
|
-
@thriftService = thriftService
|
10
|
-
|
11
|
-
#方法列表是由使用方通过参数配置的。
|
12
|
-
availableMethods.each do |methodName|
|
13
|
-
add_client_method(methodName)
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
def add_client_method(methodName)
|
19
|
-
define_singleton_method(methodName) do |params={}|
|
20
|
-
@thriftService.call(methodName, params)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|