jetra 1.1.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1841c50b2435c1f93d9282553d527b31d6799ed2a90701b4c3dad2cf614e1aa0
4
- data.tar.gz: dbafef64109519c9ea4adf70dbea208349ab18ba2855b346fdaedfb16eb33fd3
3
+ metadata.gz: 70c48ffb4371d9cf9311b1c1a8a2c57e8726f9ba1ac1d98a33e75b212621110f
4
+ data.tar.gz: 3920ca597c20574c9576cf9f483d6123af507f311faf59bce068f765ec14a930
5
5
  SHA512:
6
- metadata.gz: 3d6af2c6ed57fd5aa5270a807fdfc720335a4b717873ea396f1fcea7a58ec802be8f4cffa40811a955a078563033d6e0b449760fdfb8cd96241dd2d585c0e0e4
7
- data.tar.gz: 519e259d4550d4523b1373c455637489205c5e570210f0d38b9b1889a3372eaf4bd76f47ec356361fce70808cf485058b192e6c1c83e22934358d4b462c60e61
6
+ metadata.gz: bf0b5a6239b0fdbfcb31a86529f2c54e9f08dd48022f16d32bcd22b2e8b27555defcaf7cca980d69d9895179cc08aac15f8cdb8fd217be3c42b6825aec62b818
7
+ data.tar.gz: 24fde8d26c56b7be2ada5d76ac9de2eee8a10542a14c77793f6eda068ddc01e6029288044284912576bb6bf158646d3a5cd34be01e170f69d18d1b5ebf02e1db
@@ -4,91 +4,68 @@ require 'grpc'
4
4
  require "jetra/adapter/grpc/jetra_pb"
5
5
  require "jetra/adapter/grpc/jetra_services_pb"
6
6
 
7
+ require "google/protobuf/well_known_types"
8
+
7
9
  require 'json'
8
10
 
9
11
  module Jetra
10
12
 
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
13
+ module Grpc
19
14
 
20
- def call(request, _unused_call)
15
+ class Adapter < Interface::Service
21
16
 
22
- if @custom_block
23
- @custom_block.call(request)
17
+ def initialize(app, &custom_block)
18
+
19
+ @app = app
20
+
21
+ @custom_block = custom_block
22
+ end
23
+
24
+ def call(request, _unused_call)
25
+
26
+ if @custom_block
27
+ @custom_block.call(request)
28
+ end
29
+
30
+ res = @app.call(request.route, request.params, request.headers, request.body)
31
+
32
+ anyBody = Google::Protobuf::Any.new
33
+ anyBody.pack(res.body)
34
+
35
+ response = Response.new
36
+ response.status = res.status
37
+ response.body = anyBody
38
+ response
24
39
  end
25
-
26
- route = request.route || ""
27
40
 
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
41
  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
42
+
43
+ class Server
44
+
45
+ def initialize(handler, bind)
46
+
47
+ server = ::GRPC::RpcServer.new
48
+ server.add_http2_port(bind, :this_port_is_insecure)
49
+ server.handle(handler)
50
+
51
+ @server = server
52
+ end
53
+
54
+ def serve
55
+ @server.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
57
56
  end
58
57
  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)
58
+
59
+ class Client
60
+
61
+ def initialize(host_and_port)
62
+ @client = Interface::Stub.new(host_and_port, :this_channel_is_insecure)
63
+ end
64
+
65
+ def call(request)
66
+ @client.call(request)
67
+ end
68
+
90
69
  end
91
-
92
70
  end
93
-
94
71
  end
@@ -3,22 +3,26 @@
3
3
 
4
4
  require 'google/protobuf'
5
5
 
6
+ require 'google/protobuf/any_pb'
6
7
  Google::Protobuf::DescriptorPool.generated_pool.build do
7
8
  add_file("jetra.proto", :syntax => :proto3) do
8
- add_message "jetra.grpc.JetraRequest" do
9
+ add_message "jetra.grpc.Request" do
9
10
  optional :route, :string, 1
10
- optional :params, :string, 2
11
+ map :params, :string, :string, 2
12
+ map :headers, :string, :string, 3
13
+ optional :body, :message, 4, "google.protobuf.Any"
11
14
  end
12
- add_message "jetra.grpc.JetraResponse" do
15
+ add_message "jetra.grpc.Response" do
13
16
  optional :status, :int32, 1
14
- optional :body, :string, 2
17
+ optional :body, :message, 2, "google.protobuf.Any"
18
+ map :headers, :string, :string, 3
15
19
  end
16
20
  end
17
21
  end
18
22
 
19
23
  module Jetra
20
24
  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
25
+ Request = Google::Protobuf::DescriptorPool.generated_pool.lookup("jetra.grpc.Request").msgclass
26
+ Response = Google::Protobuf::DescriptorPool.generated_pool.lookup("jetra.grpc.Response").msgclass
23
27
  end
24
28
  end
@@ -15,7 +15,7 @@ module Jetra
15
15
  self.unmarshal_class_method = :decode
16
16
  self.service_name = 'jetra.grpc.Interface'
17
17
 
18
- rpc :Call, JetraRequest, JetraResponse
18
+ rpc :Call, Request, Response
19
19
  end
20
20
 
21
21
  Stub = Service.rpc_stub_class
@@ -20,19 +20,17 @@ module Jetra
20
20
 
21
21
  request = Rack::Request.new(env)
22
22
 
23
- params = indifferent_params(request.params)
24
-
25
- if @custom_block
26
- @custom_block.call(request, params)
27
- end
28
-
29
23
  route = request.path_info
30
24
  route.chop! if (char=route[-1]) and char=='/' # ignore last '/' char
31
25
  route[0] = '' if route[0]=="/" #remove first '/' char
32
26
 
33
- sym_route = route.to_sym
27
+ params = indifferent_params(request.params)
28
+
29
+ if @custom_block
30
+ @custom_block.call(route, params)
31
+ end
34
32
 
35
- res = @app.call(sym_route, params)
33
+ res = @app.call(route, params)
36
34
 
37
35
  result = {}
38
36
  result[:status] = res.status
@@ -15,22 +15,19 @@ module Jetra
15
15
  @app = app
16
16
 
17
17
  @custom_block = custom_block
18
-
19
18
  end
20
19
 
21
20
  def call(request)
22
21
 
23
- if @custom_block
24
- @custom_block.call(request)
25
- end
26
-
27
22
  route = request.route || ""
28
23
 
29
24
  params = parse_params(request.params)
30
25
 
31
- sym_route = route.to_sym
26
+ if @custom_block
27
+ @custom_block.call(route, params)
28
+ end
32
29
 
33
- res = @app.call(sym_route, params)
30
+ res = @app.call(route, params)
34
31
 
35
32
  response = Thrift::Response.new
36
33
  response.status = res.status
@@ -8,21 +8,24 @@ module Jetra
8
8
 
9
9
  class Request
10
10
 
11
- attr_accessor :route, :params
11
+ attr_accessor :route, :params, :headers, :body
12
12
 
13
- def initialize(route, params)
13
+ def initialize(route=nil, params=nil, headers=nil, body=nil)
14
14
  @route = route || ""
15
- @params = params || {}
15
+ @params = params
16
+ @headers = headers
17
+ @body = body
16
18
  end
17
19
  end
18
20
 
19
21
  class Response
20
22
 
21
- attr_accessor :status, :body
23
+ attr_accessor :status, :body, :headers
22
24
 
23
- def initialize(status=-1, body="")
25
+ def initialize(status=nil, body=nil, headers=nil)
24
26
  @status = status.to_i
25
27
  @body = body
28
+ @headers = headers
26
29
  end
27
30
 
28
31
  def finish
@@ -32,18 +35,22 @@ module Jetra
32
35
 
33
36
  class Base
34
37
 
38
+ Settings = {}
39
+ Settings[:env] = "development"
40
+
35
41
  attr_accessor :request, :response, :params
36
42
 
37
- def call(route, params)
38
- dup.call!(route, params)
43
+ def call(route=nil, params=nil, headers=nil, body=nil)
44
+ request = Request.new(route, params, headers, body)
45
+ dup.call!(request)
39
46
  end
40
47
 
41
- def call!(route, params)
48
+ def call!(request)
42
49
 
43
- @request = Request.new(route, params)
50
+ @request = request
44
51
  @response = Response.new
45
52
 
46
- @params = indifferent_params(@request.params)
53
+ @params = request.params
47
54
 
48
55
  invoke { dispatch! }
49
56
 
@@ -54,23 +61,6 @@ module Jetra
54
61
  self.class
55
62
  end
56
63
 
57
- def indifferent_params(object)
58
- case object
59
- when Hash
60
- newHash = indifferent_hash
61
- object.each { |key, value| newHash[key] = indifferent_params(value) }
62
- newHash
63
- when Array
64
- object.map { |item| indifferent_params(item) }
65
- else
66
- object
67
- end
68
- end
69
-
70
- def indifferent_hash
71
- Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
72
- end
73
-
74
64
  def invoke
75
65
  res = catch(Halt) { yield }
76
66
 
@@ -198,8 +188,8 @@ module Jetra
198
188
  @prototype ||= new
199
189
  end
200
190
 
201
- def call(route, params={})
202
- prototype.call(route, params)
191
+ def call(route=nil, params=nil, headers=nil, body=nil)
192
+ prototype.call(route, params, headers, body)
203
193
  end
204
194
 
205
195
  def before(&block)
@@ -214,7 +204,8 @@ module Jetra
214
204
  @filters[type] << compile!(&block)
215
205
  end
216
206
 
217
- def route(symbol, &block)
207
+ def route(string, &block)
208
+ symbol = string.to_sym
218
209
  block ||= Proc.new { method(symbol).call }
219
210
  @routes[symbol] = compile!(&block)
220
211
  end
@@ -303,6 +294,8 @@ module Jetra
303
294
 
304
295
  error do |boom|
305
296
 
297
+ raise boom #TODO 因为服务类型不同,错误返回就不同,没法统一默认异常返回结果
298
+
306
299
  boommsg = "#{boom.class} - #{boom.message}"
307
300
 
308
301
  if boom.class == Jetra::NotFoundException
@@ -1,5 +1,5 @@
1
1
  module Jetra
2
2
 
3
- Version = "1.1.0"
3
+ Version = "1.2.0"
4
4
 
5
5
  end
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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeffrey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-21 00:00:00.000000000 Z
11
+ date: 2019-10-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: micro DSL
14
14
  email: