grpc-rest 0.3.1 → 0.5.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/.github/workflows/release.yml +31 -0
- data/CHANGELOG +10 -0
- data/Gemfile.lock +156 -118
- data/README.md +28 -62
- data/Rakefile +3 -0
- data/lib/generator/controller.rb +51 -0
- data/lib/generator/method.rb +3 -3
- data/lib/generator/service.rb +10 -20
- data/lib/grpc_rest/railtie.rb +19 -0
- data/lib/grpc_rest/version.rb +1 -1
- data/lib/grpc_rest.rb +24 -12
- data/spec/grpc_rest_spec.rb +1 -1
- data/spec/gruf_spec.rb +1 -1
- data/spec/spec_helper.rb +11 -12
- data/spec/test_service_services_pb.rb +1 -2
- metadata +7 -29
- data/lib/generator/controller.rb.erb +0 -27
- data/lib/generator/grpc.rb.erb +0 -9
- data/lib/generator/output.rb +0 -28
- data/lib/generator/plugin_pb.rb +0 -25
- data/spec/__snapshots__/no_service.snap +0 -0
- data/spec/__snapshots__/service.snap +0 -73
- data/spec/gen/test_pb.rb +0 -15
- data/spec/gen/test_service_pb.rb +0 -23
- data/spec/gen/test_service_services_pb.rb +0 -26
- data/spec/generator_spec.rb +0 -49
- data/spec/testdata/base/app/controllers/my_service_controller.rb +0 -58
- data/spec/testdata/base/config/routes/grpc.rb +0 -10
- data/spec/testdata/no_service/.keep +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '097d1a366f7eb41bd6a4b1e68f8cacfa84e74013aaed85d3348a8be300503f64'
|
4
|
+
data.tar.gz: ef6e2ca8a7f43577a51d0fb9b85ad2ed6576f8591b33520732c6a62dd976daf2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a181f74f58f23e2c89b73e24833197e365a167cdea63b55a31635d06349062becc15f9b1ead3572801514587be288fa5aca02f180b8c519a505350ea8e475f5
|
7
|
+
data.tar.gz: fd0c20102f445578d5c21a5a984e95323fbdc9a5d102c8e9d8ccadd17a9b9196896e0a6e6c55ac3fd1c98f6e3b3b6b4669e4ec5573970d1dbf1c1663b9fab46d
|
@@ -0,0 +1,31 @@
|
|
1
|
+
name: Release Gem
|
2
|
+
on:
|
3
|
+
push:
|
4
|
+
branches:
|
5
|
+
- main
|
6
|
+
tags:
|
7
|
+
- 'v*.*.*' # Matches semantic versioning tags like v1.0.0
|
8
|
+
workflow_dispatch: # Allows manual triggering of the workflow
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
push:
|
12
|
+
name: Push gem to RubyGems.org
|
13
|
+
runs-on: ubuntu-latest
|
14
|
+
|
15
|
+
permissions:
|
16
|
+
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
|
17
|
+
contents: write # IMPORTANT: this permission is required for `rake release` to push the release tag
|
18
|
+
|
19
|
+
steps:
|
20
|
+
# Set up
|
21
|
+
- uses: actions/checkout@v4
|
22
|
+
with:
|
23
|
+
persist-credentials: false
|
24
|
+
- name: Set up Ruby
|
25
|
+
uses: ruby/setup-ruby@v1
|
26
|
+
with:
|
27
|
+
bundler-cache: true
|
28
|
+
ruby-version: ruby
|
29
|
+
|
30
|
+
# Release
|
31
|
+
- uses: rubygems/release-gem@v1
|
data/CHANGELOG
CHANGED
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## UNRELEASED
|
9
9
|
|
10
|
+
# 0.5.0 - 2025-08-25
|
11
|
+
|
12
|
+
- ***MAJOR CHANGE***: `grpc-rest` now no longer requires any code generation. All functionality is dynamic and loads on startup.
|
13
|
+
- Added `GrpcRest.services` configuration option.
|
14
|
+
|
15
|
+
# 0.4.0 - 2025-07-08
|
16
|
+
|
17
|
+
- Feature: Forward request headers to the gRPC call as metadata.
|
18
|
+
- ***BREAKING CHANGE***: Update interceptors to pass a Gruf::Controller::Request instead of the raw gRPC request object. This allows e.g. checking metadata inside the interceptor.
|
19
|
+
|
10
20
|
# 0.3.1 - 2025-07-02
|
11
21
|
- Fix: Include full error backtrace in logs
|
12
22
|
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
grpc-rest (0.
|
4
|
+
grpc-rest (0.5.0)
|
5
5
|
google-protobuf (>= 4.30.2)
|
6
6
|
grpc
|
7
7
|
rails (>= 6.0)
|
@@ -9,98 +9,109 @@ PATH
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
actioncable (
|
13
|
-
actionpack (=
|
14
|
-
activesupport (=
|
12
|
+
actioncable (8.0.2.1)
|
13
|
+
actionpack (= 8.0.2.1)
|
14
|
+
activesupport (= 8.0.2.1)
|
15
15
|
nio4r (~> 2.0)
|
16
16
|
websocket-driver (>= 0.6.1)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
actionview (= 7.0.8.1)
|
39
|
-
activesupport (= 7.0.8.1)
|
40
|
-
rack (~> 2.0, >= 2.2.4)
|
17
|
+
zeitwerk (~> 2.6)
|
18
|
+
actionmailbox (8.0.2.1)
|
19
|
+
actionpack (= 8.0.2.1)
|
20
|
+
activejob (= 8.0.2.1)
|
21
|
+
activerecord (= 8.0.2.1)
|
22
|
+
activestorage (= 8.0.2.1)
|
23
|
+
activesupport (= 8.0.2.1)
|
24
|
+
mail (>= 2.8.0)
|
25
|
+
actionmailer (8.0.2.1)
|
26
|
+
actionpack (= 8.0.2.1)
|
27
|
+
actionview (= 8.0.2.1)
|
28
|
+
activejob (= 8.0.2.1)
|
29
|
+
activesupport (= 8.0.2.1)
|
30
|
+
mail (>= 2.8.0)
|
31
|
+
rails-dom-testing (~> 2.2)
|
32
|
+
actionpack (8.0.2.1)
|
33
|
+
actionview (= 8.0.2.1)
|
34
|
+
activesupport (= 8.0.2.1)
|
35
|
+
nokogiri (>= 1.8.5)
|
36
|
+
rack (>= 2.2.4)
|
37
|
+
rack-session (>= 1.0.1)
|
41
38
|
rack-test (>= 0.6.3)
|
42
|
-
rails-dom-testing (~> 2.
|
43
|
-
rails-html-sanitizer (~> 1.
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
39
|
+
rails-dom-testing (~> 2.2)
|
40
|
+
rails-html-sanitizer (~> 1.6)
|
41
|
+
useragent (~> 0.16)
|
42
|
+
actiontext (8.0.2.1)
|
43
|
+
actionpack (= 8.0.2.1)
|
44
|
+
activerecord (= 8.0.2.1)
|
45
|
+
activestorage (= 8.0.2.1)
|
46
|
+
activesupport (= 8.0.2.1)
|
49
47
|
globalid (>= 0.6.0)
|
50
48
|
nokogiri (>= 1.8.5)
|
51
|
-
actionview (
|
52
|
-
activesupport (=
|
49
|
+
actionview (8.0.2.1)
|
50
|
+
activesupport (= 8.0.2.1)
|
53
51
|
builder (~> 3.1)
|
54
|
-
erubi (~> 1.
|
55
|
-
rails-dom-testing (~> 2.
|
56
|
-
rails-html-sanitizer (~> 1.
|
57
|
-
activejob (
|
58
|
-
activesupport (=
|
52
|
+
erubi (~> 1.11)
|
53
|
+
rails-dom-testing (~> 2.2)
|
54
|
+
rails-html-sanitizer (~> 1.6)
|
55
|
+
activejob (8.0.2.1)
|
56
|
+
activesupport (= 8.0.2.1)
|
59
57
|
globalid (>= 0.3.6)
|
60
|
-
activemodel (
|
61
|
-
activesupport (=
|
62
|
-
activerecord (
|
63
|
-
activemodel (=
|
64
|
-
activesupport (=
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
58
|
+
activemodel (8.0.2.1)
|
59
|
+
activesupport (= 8.0.2.1)
|
60
|
+
activerecord (8.0.2.1)
|
61
|
+
activemodel (= 8.0.2.1)
|
62
|
+
activesupport (= 8.0.2.1)
|
63
|
+
timeout (>= 0.4.0)
|
64
|
+
activestorage (8.0.2.1)
|
65
|
+
actionpack (= 8.0.2.1)
|
66
|
+
activejob (= 8.0.2.1)
|
67
|
+
activerecord (= 8.0.2.1)
|
68
|
+
activesupport (= 8.0.2.1)
|
70
69
|
marcel (~> 1.0)
|
71
|
-
|
72
|
-
|
73
|
-
|
70
|
+
activesupport (8.0.2.1)
|
71
|
+
base64
|
72
|
+
benchmark (>= 0.3)
|
73
|
+
bigdecimal
|
74
|
+
concurrent-ruby (~> 1.0, >= 1.3.1)
|
75
|
+
connection_pool (>= 2.2.5)
|
76
|
+
drb
|
74
77
|
i18n (>= 1.6, < 2)
|
78
|
+
logger (>= 1.4.2)
|
75
79
|
minitest (>= 5.1)
|
76
|
-
|
80
|
+
securerandom (>= 0.3)
|
81
|
+
tzinfo (~> 2.0, >= 2.0.5)
|
82
|
+
uri (>= 0.13.1)
|
77
83
|
awesome_print (1.9.2)
|
78
|
-
|
79
|
-
|
84
|
+
base64 (0.3.0)
|
85
|
+
benchmark (0.4.1)
|
86
|
+
bigdecimal (3.2.2)
|
87
|
+
builder (3.3.0)
|
80
88
|
byebug (12.0.0)
|
81
|
-
concurrent-ruby (1.
|
89
|
+
concurrent-ruby (1.3.5)
|
90
|
+
connection_pool (2.5.3)
|
82
91
|
crass (1.0.6)
|
83
|
-
date (3.
|
84
|
-
diff-lcs (1.
|
85
|
-
|
92
|
+
date (3.4.1)
|
93
|
+
diff-lcs (1.6.2)
|
94
|
+
drb (2.2.3)
|
95
|
+
erb (5.0.2)
|
96
|
+
erubi (1.13.1)
|
86
97
|
globalid (1.2.1)
|
87
98
|
activesupport (>= 6.1)
|
88
|
-
google-protobuf (4.
|
99
|
+
google-protobuf (4.32.0-arm64-darwin)
|
89
100
|
bigdecimal
|
90
101
|
rake (>= 13)
|
91
|
-
google-protobuf (4.
|
102
|
+
google-protobuf (4.32.0-x86_64-linux-gnu)
|
92
103
|
bigdecimal
|
93
104
|
rake (>= 13)
|
94
|
-
googleapis-common-protos-types (1.
|
105
|
+
googleapis-common-protos-types (1.20.0)
|
95
106
|
google-protobuf (>= 3.18, < 5.a)
|
96
|
-
grpc (1.
|
107
|
+
grpc (1.74.1-arm64-darwin)
|
97
108
|
google-protobuf (>= 3.25, < 5.0)
|
98
109
|
googleapis-common-protos-types (~> 1.0)
|
99
|
-
grpc (1.
|
110
|
+
grpc (1.74.1-x86_64-linux-gnu)
|
100
111
|
google-protobuf (>= 3.25, < 5.0)
|
101
112
|
googleapis-common-protos-types (~> 1.0)
|
102
|
-
grpc-tools (1.
|
103
|
-
gruf (2.
|
113
|
+
grpc-tools (1.74.1)
|
114
|
+
gruf (2.21.1)
|
104
115
|
activesupport (> 4)
|
105
116
|
concurrent-ruby (> 1)
|
106
117
|
grpc (~> 1.10)
|
@@ -108,10 +119,16 @@ GEM
|
|
108
119
|
json (>= 2.3)
|
109
120
|
slop (>= 4.6)
|
110
121
|
zeitwerk (>= 2)
|
111
|
-
i18n (1.14.
|
122
|
+
i18n (1.14.7)
|
112
123
|
concurrent-ruby (~> 1.0)
|
113
|
-
|
114
|
-
|
124
|
+
io-console (0.8.1)
|
125
|
+
irb (1.15.2)
|
126
|
+
pp (>= 0.6.0)
|
127
|
+
rdoc (>= 4.0.0)
|
128
|
+
reline (>= 0.4.2)
|
129
|
+
json (2.13.2)
|
130
|
+
logger (1.7.0)
|
131
|
+
loofah (2.24.1)
|
115
132
|
crass (~> 1.0.2)
|
116
133
|
nokogiri (>= 1.12.0)
|
117
134
|
mail (2.8.1)
|
@@ -120,72 +137,88 @@ GEM
|
|
120
137
|
net-pop
|
121
138
|
net-smtp
|
122
139
|
marcel (1.0.4)
|
123
|
-
method_source (1.0.0)
|
124
140
|
mini_mime (1.1.5)
|
125
|
-
minitest (5.
|
126
|
-
net-imap (0.
|
141
|
+
minitest (5.25.5)
|
142
|
+
net-imap (0.5.9)
|
127
143
|
date
|
128
144
|
net-protocol
|
129
145
|
net-pop (0.1.2)
|
130
146
|
net-protocol
|
131
147
|
net-protocol (0.2.2)
|
132
148
|
timeout
|
133
|
-
net-smtp (0.5.
|
149
|
+
net-smtp (0.5.1)
|
134
150
|
net-protocol
|
135
|
-
nio4r (2.7.
|
136
|
-
nokogiri (1.
|
151
|
+
nio4r (2.7.4)
|
152
|
+
nokogiri (1.18.9-arm64-darwin)
|
137
153
|
racc (~> 1.4)
|
138
|
-
nokogiri (1.
|
154
|
+
nokogiri (1.18.9-x86_64-linux-gnu)
|
139
155
|
racc (~> 1.4)
|
140
|
-
|
141
|
-
|
142
|
-
|
156
|
+
pp (0.6.2)
|
157
|
+
prettyprint
|
158
|
+
prettyprint (0.2.0)
|
159
|
+
psych (5.2.6)
|
160
|
+
date
|
161
|
+
stringio
|
162
|
+
racc (1.8.1)
|
163
|
+
rack (3.2.0)
|
164
|
+
rack-session (2.1.1)
|
165
|
+
base64 (>= 0.1.0)
|
166
|
+
rack (>= 3.0.0)
|
167
|
+
rack-test (2.2.0)
|
143
168
|
rack (>= 1.3)
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
169
|
+
rackup (2.2.1)
|
170
|
+
rack (>= 3)
|
171
|
+
rails (8.0.2.1)
|
172
|
+
actioncable (= 8.0.2.1)
|
173
|
+
actionmailbox (= 8.0.2.1)
|
174
|
+
actionmailer (= 8.0.2.1)
|
175
|
+
actionpack (= 8.0.2.1)
|
176
|
+
actiontext (= 8.0.2.1)
|
177
|
+
actionview (= 8.0.2.1)
|
178
|
+
activejob (= 8.0.2.1)
|
179
|
+
activemodel (= 8.0.2.1)
|
180
|
+
activerecord (= 8.0.2.1)
|
181
|
+
activestorage (= 8.0.2.1)
|
182
|
+
activesupport (= 8.0.2.1)
|
156
183
|
bundler (>= 1.15.0)
|
157
|
-
railties (=
|
158
|
-
rails-dom-testing (2.
|
184
|
+
railties (= 8.0.2.1)
|
185
|
+
rails-dom-testing (2.3.0)
|
159
186
|
activesupport (>= 5.0.0)
|
160
187
|
minitest
|
161
188
|
nokogiri (>= 1.6)
|
162
|
-
rails-html-sanitizer (1.6.
|
189
|
+
rails-html-sanitizer (1.6.2)
|
163
190
|
loofah (~> 2.21)
|
164
|
-
nokogiri (
|
165
|
-
railties (
|
166
|
-
actionpack (=
|
167
|
-
activesupport (=
|
168
|
-
|
191
|
+
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
|
192
|
+
railties (8.0.2.1)
|
193
|
+
actionpack (= 8.0.2.1)
|
194
|
+
activesupport (= 8.0.2.1)
|
195
|
+
irb (~> 1.13)
|
196
|
+
rackup (>= 1.0.0)
|
169
197
|
rake (>= 12.2)
|
170
|
-
thor (~> 1.0)
|
171
|
-
zeitwerk (~> 2.
|
172
|
-
rake (13.
|
173
|
-
|
198
|
+
thor (~> 1.0, >= 1.2.2)
|
199
|
+
zeitwerk (~> 2.6)
|
200
|
+
rake (13.3.0)
|
201
|
+
rdoc (6.14.2)
|
202
|
+
erb
|
203
|
+
psych (>= 4.0.0)
|
204
|
+
reline (0.6.2)
|
205
|
+
io-console (~> 0.5)
|
206
|
+
rspec (3.13.1)
|
174
207
|
rspec-core (~> 3.13.0)
|
175
208
|
rspec-expectations (~> 3.13.0)
|
176
209
|
rspec-mocks (~> 3.13.0)
|
177
|
-
rspec-core (3.13.
|
210
|
+
rspec-core (3.13.5)
|
178
211
|
rspec-support (~> 3.13.0)
|
179
|
-
rspec-expectations (3.13.
|
212
|
+
rspec-expectations (3.13.5)
|
180
213
|
diff-lcs (>= 1.2.0, < 2.0)
|
181
214
|
rspec-support (~> 3.13.0)
|
182
|
-
rspec-mocks (3.13.
|
215
|
+
rspec-mocks (3.13.5)
|
183
216
|
diff-lcs (>= 1.2.0, < 2.0)
|
184
217
|
rspec-support (~> 3.13.0)
|
185
|
-
rspec-rails (
|
186
|
-
actionpack (>=
|
187
|
-
activesupport (>=
|
188
|
-
railties (>=
|
218
|
+
rspec-rails (8.0.2)
|
219
|
+
actionpack (>= 7.2)
|
220
|
+
activesupport (>= 7.2)
|
221
|
+
railties (>= 7.2)
|
189
222
|
rspec-core (~> 3.13)
|
190
223
|
rspec-expectations (~> 3.13)
|
191
224
|
rspec-mocks (~> 3.13)
|
@@ -193,16 +226,21 @@ GEM
|
|
193
226
|
rspec-snapshot (2.0.3)
|
194
227
|
awesome_print (> 1.0.0)
|
195
228
|
rspec (> 3.0.0)
|
196
|
-
rspec-support (3.13.
|
229
|
+
rspec-support (3.13.5)
|
230
|
+
securerandom (0.4.1)
|
197
231
|
slop (4.10.1)
|
198
|
-
|
199
|
-
|
232
|
+
stringio (3.1.7)
|
233
|
+
thor (1.4.0)
|
234
|
+
timeout (0.4.3)
|
200
235
|
tzinfo (2.0.6)
|
201
236
|
concurrent-ruby (~> 1.0)
|
202
|
-
|
237
|
+
uri (1.0.3)
|
238
|
+
useragent (0.16.11)
|
239
|
+
websocket-driver (0.8.0)
|
240
|
+
base64
|
203
241
|
websocket-extensions (>= 0.1.0)
|
204
242
|
websocket-extensions (0.1.5)
|
205
|
-
zeitwerk (2.
|
243
|
+
zeitwerk (2.7.3)
|
206
244
|
|
207
245
|
PLATFORMS
|
208
246
|
arm64-darwin
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# grpc-rest
|
2
|
-
Generate Rails routes and controllers from protobuf files.
|
2
|
+
Generate dynamic Rails routes and controllers from protobuf files.
|
3
3
|
|
4
4
|
grpc-rest allows you to have a single codebase that serves both gRPC and classic Rails routes, with
|
5
5
|
a single handler (the gRPC handler) for both types of requests. It will add Rails routes to your
|
@@ -12,14 +12,13 @@ the default port for Rails is 3000 and for gRPC is 9001).
|
|
12
12
|
|
13
13
|
With this, you get the automatic client code generation via Swagger and gRPC, the input validation that's automatic with gRPC, and the ease of use of tools like `curl` and Postman with REST.
|
14
14
|
|
15
|
-
##
|
15
|
+
## Upgrading to 0.5
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
*
|
20
|
-
* The
|
21
|
-
|
22
|
-
The protobuf generator uses the same annotations as [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway). This also gives you the benefit of being able to generate Swagger files by using protoc.
|
17
|
+
Before 0.5, you needed to generate Rails controller and route files. As of 0.5, these are no longer necessary and you can remove the following:
|
18
|
+
* Generated files in `app/controllers`
|
19
|
+
* Generated files in `config/routes`
|
20
|
+
* The `draw(:grpc)` line in `config/routes.rb`
|
21
|
+
* Add `require 'grpc_rest'` to your `config/application.rb`
|
23
22
|
|
24
23
|
## Installation
|
25
24
|
|
@@ -29,9 +28,17 @@ Add the following to your `Gemfile`:
|
|
29
28
|
gem 'grpc-rest'
|
30
29
|
```
|
31
30
|
|
32
|
-
and run `bundle install`.
|
31
|
+
and run `bundle install`.
|
33
32
|
|
34
|
-
|
33
|
+
Then add this to your `config/application.rb`:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'grpc_rest'
|
37
|
+
```
|
38
|
+
|
39
|
+
## Routes
|
40
|
+
|
41
|
+
`grpc-rest` uses the same annotations as [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway) to define routes. This also gives you the benefit of being able to generate Swagger files by using protoc.
|
35
42
|
|
36
43
|
## Example
|
37
44
|
|
@@ -61,7 +68,7 @@ service ExampleService {
|
|
61
68
|
}
|
62
69
|
```
|
63
70
|
|
64
|
-
|
71
|
+
You need to generate the Ruby files from this, the same way you would for any Protobuf Ruby code. You can do this with plain `protoc`, but it's much easier to handle if you use [buf](https://buf.build/). Here's an example `buf.gen.yaml` file:
|
65
72
|
|
66
73
|
```yaml
|
67
74
|
version: v1
|
@@ -76,59 +83,16 @@ plugins:
|
|
76
83
|
out: app/gen
|
77
84
|
opt:
|
78
85
|
- paths=source_relative
|
79
|
-
- name: rails
|
80
|
-
out: .
|
81
|
-
opt:
|
82
|
-
# this should be the same directory as the output of your Ruby plugin
|
83
|
-
- require=app/gen
|
84
86
|
```
|
85
87
|
|
86
|
-
Then, you can run `buf generate` to generate the Ruby files.
|
87
|
-
* the Protobuf Ruby files for grpc, in `app/gen`
|
88
|
-
* A new route file, in `config/routes/grpc.rb`
|
89
|
-
* A new controller file, in `app/controllers`.
|
88
|
+
Then, you can run `buf generate` to generate the Ruby files.
|
90
89
|
|
91
|
-
The
|
90
|
+
The dynamic route will act like this:
|
92
91
|
|
93
92
|
```ruby
|
94
93
|
get "example/:name", to: "example#get_example"
|
95
94
|
```
|
96
95
|
|
97
|
-
and the generated controller will look like this:
|
98
|
-
|
99
|
-
```ruby
|
100
|
-
require 'services/example/example_services_pb'
|
101
|
-
class ExampleServiceController < ActionController::Base
|
102
|
-
protect_from_forgery with: :null_session
|
103
|
-
|
104
|
-
METHOD_PARAM_MAP = {
|
105
|
-
|
106
|
-
"example" => [
|
107
|
-
{name: "name", val: nil, split_name:["name"]},
|
108
|
-
],
|
109
|
-
}.freeze
|
110
|
-
|
111
|
-
rescue_from StandardError do |e|
|
112
|
-
render json: GrpcRest.error_msg(e)
|
113
|
-
end
|
114
|
-
|
115
|
-
def example
|
116
|
-
grpc_request = Services::Example::ExampleRequest.new
|
117
|
-
GrpcRest.assign_params(grpc_request, "/example/{name}", "", request.parameters)
|
118
|
-
render json: GrpcRest.send_request("Services::Example::ExampleService", "example", grpc_request)
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
```
|
123
|
-
|
124
|
-
To power it on, all you have to do is add the following to your `config/routes.rb`:
|
125
|
-
|
126
|
-
```ruby
|
127
|
-
Rails.application.routes.draw do
|
128
|
-
draw(:grpc) # Add this line
|
129
|
-
end
|
130
|
-
```
|
131
|
-
|
132
96
|
## Caveats
|
133
97
|
|
134
98
|
This gem does not currently support the full path expression capabilities of grpc-gateway or the Google [http proto](https://github.com/googleapis/googleapis/blob/master/google/api/http.proto). It only supports very basic single wildcard globbing (`*`). Contributions are welcome for more complex cases if they are needed.
|
@@ -177,6 +141,14 @@ end
|
|
177
141
|
|
178
142
|
### Configuration
|
179
143
|
|
144
|
+
If you're using `gruf`, grpc-rest will automatically collect the known Gruf controllers. If you're not using it, you have to tell grpc-rest which services you want to generate for by setting:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
GrpcRest.services = [
|
148
|
+
Example::ExampleService::Service
|
149
|
+
]
|
150
|
+
```
|
151
|
+
|
180
152
|
GrpcRest currently has one configuration option, `strict_mode`, which defaults to false. When set to true, any JSON request that includes an unknown field will be rejected.
|
181
153
|
|
182
154
|
```ruby
|
@@ -193,12 +165,6 @@ To regenerate Ruby protobuf code for tests, install the `grpc-tools` gem and run
|
|
193
165
|
grpc_tools_ruby_protoc -I=./spec/testdata --proto_path=./spec/google-deps --ruby_out=./spec --grpc_out=./spec ./spec/testdata/test_service.proto
|
194
166
|
```
|
195
167
|
|
196
|
-
To regenerate the controller and route files for tests, run:
|
197
|
-
|
198
|
-
```
|
199
|
-
bundle exec grpc_tools_ruby_protoc -I=./spec/testdata --proto_path=./spec/google-deps --rails_out=spec/testdata/base --rails_opt=require=spec/gen ./spec/testdata/test_service.proto
|
200
|
-
```
|
201
|
-
|
202
168
|
## License
|
203
169
|
|
204
170
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'google/protobuf'
|
2
|
+
require 'grpc'
|
3
|
+
|
4
|
+
module GrpcRestControllers
|
5
|
+
end
|
6
|
+
|
7
|
+
module GrpcRest
|
8
|
+
class Controller < ActionController::Base
|
9
|
+
protect_from_forgery with: :null_session
|
10
|
+
|
11
|
+
rescue_from StandardError do |e|
|
12
|
+
render json: GrpcRest.error_msg(e), status: :internal_server_error
|
13
|
+
end
|
14
|
+
rescue_from GRPC::BadStatus do |e|
|
15
|
+
render json: GrpcRest.error_msg(e), status: GrpcRest.grpc_http_status(e.code)
|
16
|
+
end
|
17
|
+
rescue_from ActionDispatch::Http::Parameters::ParseError, Google::Protobuf::TypeError do |e|
|
18
|
+
render json: GrpcRest.error_msg(e), status: :bad_request
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.controller(service)
|
23
|
+
# e.g. GrpcRestControllers::MyServiceController
|
24
|
+
klass = Class.new(GrpcRest::Controller) do
|
25
|
+
service.methods.each do |method|
|
26
|
+
# e.g. def my_service_test_method
|
27
|
+
define_method(method.name) do
|
28
|
+
parameters = request.parameters.to_h.deep_transform_keys(&:underscore).
|
29
|
+
except('controller', 'action', service.name.underscore)
|
30
|
+
grpc_request = GrpcRest.init_request(method.request_type, parameters)
|
31
|
+
GrpcRest.assign_params(grpc_request,
|
32
|
+
method.path_info.map(&:to_h),
|
33
|
+
method.option_body,
|
34
|
+
request.parameters)
|
35
|
+
render json: GrpcRest.send_request(service.service,
|
36
|
+
method.name.underscore,
|
37
|
+
grpc_request,
|
38
|
+
method.rest_options,
|
39
|
+
headers: request.headers)
|
40
|
+
end
|
41
|
+
Rails.application.routes.append do
|
42
|
+
service_name = service.name.demodulize.underscore
|
43
|
+
path = "grpc_rest_controllers/#{service_name}##{method.name.underscore}"
|
44
|
+
# e.g. get '/my_service/test_method' => '/grpc_rest_controllers/my_service#test_method'
|
45
|
+
self.send(method.http_method, method.sanitized_path => path)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
GrpcRestControllers.const_set("#{service.name.demodulize}Controller", klass)
|
50
|
+
end
|
51
|
+
end
|
data/lib/generator/method.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
4
|
-
class
|
3
|
+
module GrpcRest
|
4
|
+
class GeneratedMethod
|
5
5
|
PathInfo = Struct.new(:name, :val, :split_name)
|
6
6
|
|
7
7
|
attr_accessor :http_method, :path, :path_info, :rest_options
|
@@ -22,7 +22,7 @@ module ProtocGenRails
|
|
22
22
|
|
23
23
|
# @return [String]
|
24
24
|
def request_type
|
25
|
-
@method.input_type.
|
25
|
+
@method.input_type.msgclass
|
26
26
|
end
|
27
27
|
|
28
28
|
# @return [String]
|