grpc_for_rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +175 -0
- data/LICENSE.md +22 -0
- data/README.md +68 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/grpc_server +60 -0
- data/grpc_for_rails.gemspec +31 -0
- data/lib/grpc_for_rails.rb +7 -0
- data/lib/grpc_for_rails/railtie.rb +20 -0
- data/lib/grpc_for_rails/server.rb +168 -0
- data/lib/grpc_for_rails/version.rb +3 -0
- data/lib/rails/generators/install_generator.rb +18 -0
- data/lib/rails/generators/templates/grpc_server.yml +17 -0
- data/lib/tasks/grpc.rake +7 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9ad7d7141f21cb16772bad6f19fbdf4c3a4a0c93
|
4
|
+
data.tar.gz: b26493e1add520441cb47c379b8dc32bbd4ed04a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d2f9df89f4ebf4c4d2b921b29f66d9c6e3b95b25d187137ca6daebb7b11752344731b35a30d5dc28d85c70141e9e889862a6d2b6de90b5865a619ef6b3b387b
|
7
|
+
data.tar.gz: c9e804aa14f79c4517f0338c34081b0751ee412aae03ca6907e6483b8e29923aa89de98cb270f0371c1b6581f102b4d01afb4c428ac8834b812a43c0b2e39c62
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.1
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 xiewenwei@gmail.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
data/Gemfile.lock
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
grpc_for_rails (0.1.0)
|
5
|
+
grpc (~> 1.2)
|
6
|
+
grpc-tools (~> 1.2)
|
7
|
+
railties (>= 4.2.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
actioncable (5.1.4)
|
13
|
+
actionpack (= 5.1.4)
|
14
|
+
nio4r (~> 2.0)
|
15
|
+
websocket-driver (~> 0.6.1)
|
16
|
+
actionmailer (5.1.4)
|
17
|
+
actionpack (= 5.1.4)
|
18
|
+
actionview (= 5.1.4)
|
19
|
+
activejob (= 5.1.4)
|
20
|
+
mail (~> 2.5, >= 2.5.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
actionpack (5.1.4)
|
23
|
+
actionview (= 5.1.4)
|
24
|
+
activesupport (= 5.1.4)
|
25
|
+
rack (~> 2.0)
|
26
|
+
rack-test (>= 0.6.3)
|
27
|
+
rails-dom-testing (~> 2.0)
|
28
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
29
|
+
actionview (5.1.4)
|
30
|
+
activesupport (= 5.1.4)
|
31
|
+
builder (~> 3.1)
|
32
|
+
erubi (~> 1.4)
|
33
|
+
rails-dom-testing (~> 2.0)
|
34
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
35
|
+
activejob (5.1.4)
|
36
|
+
activesupport (= 5.1.4)
|
37
|
+
globalid (>= 0.3.6)
|
38
|
+
activemodel (5.1.4)
|
39
|
+
activesupport (= 5.1.4)
|
40
|
+
activerecord (5.1.4)
|
41
|
+
activemodel (= 5.1.4)
|
42
|
+
activesupport (= 5.1.4)
|
43
|
+
arel (~> 8.0)
|
44
|
+
activesupport (5.1.4)
|
45
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
46
|
+
i18n (~> 0.7)
|
47
|
+
minitest (~> 5.1)
|
48
|
+
tzinfo (~> 1.1)
|
49
|
+
addressable (2.5.2)
|
50
|
+
public_suffix (>= 2.0.2, < 4.0)
|
51
|
+
arel (8.0.0)
|
52
|
+
builder (3.2.3)
|
53
|
+
concurrent-ruby (1.0.5)
|
54
|
+
crass (1.0.3)
|
55
|
+
erubi (1.7.0)
|
56
|
+
faraday (0.13.1)
|
57
|
+
multipart-post (>= 1.2, < 3)
|
58
|
+
ffi (1.9.18)
|
59
|
+
globalid (0.4.1)
|
60
|
+
activesupport (>= 4.2.0)
|
61
|
+
google-protobuf (3.5.0)
|
62
|
+
googleapis-common-protos-types (1.0.1)
|
63
|
+
google-protobuf (~> 3.0)
|
64
|
+
googleauth (0.6.2)
|
65
|
+
faraday (~> 0.12)
|
66
|
+
jwt (>= 1.4, < 3.0)
|
67
|
+
logging (~> 2.0)
|
68
|
+
memoist (~> 0.12)
|
69
|
+
multi_json (~> 1.11)
|
70
|
+
os (~> 0.9)
|
71
|
+
signet (~> 0.7)
|
72
|
+
grpc (1.7.3)
|
73
|
+
google-protobuf (~> 3.1)
|
74
|
+
googleapis-common-protos-types (~> 1.0.0)
|
75
|
+
googleauth (>= 0.5.1, < 0.7)
|
76
|
+
grpc-tools (1.8.0)
|
77
|
+
i18n (0.9.1)
|
78
|
+
concurrent-ruby (~> 1.0)
|
79
|
+
jbuilder (2.7.0)
|
80
|
+
activesupport (>= 4.2.0)
|
81
|
+
multi_json (>= 1.2)
|
82
|
+
jwt (2.1.0)
|
83
|
+
listen (3.1.5)
|
84
|
+
rb-fsevent (~> 0.9, >= 0.9.4)
|
85
|
+
rb-inotify (~> 0.9, >= 0.9.7)
|
86
|
+
ruby_dep (~> 1.2)
|
87
|
+
little-plugger (1.1.4)
|
88
|
+
logging (2.2.2)
|
89
|
+
little-plugger (~> 1.1)
|
90
|
+
multi_json (~> 1.10)
|
91
|
+
loofah (2.1.1)
|
92
|
+
crass (~> 1.0.2)
|
93
|
+
nokogiri (>= 1.5.9)
|
94
|
+
mail (2.7.0)
|
95
|
+
mini_mime (>= 0.1.1)
|
96
|
+
memoist (0.16.0)
|
97
|
+
method_source (0.9.0)
|
98
|
+
mini_mime (1.0.0)
|
99
|
+
mini_portile2 (2.3.0)
|
100
|
+
minitest (5.10.3)
|
101
|
+
multi_json (1.12.2)
|
102
|
+
multipart-post (2.0.0)
|
103
|
+
nio4r (2.1.0)
|
104
|
+
nokogiri (1.8.1)
|
105
|
+
mini_portile2 (~> 2.3.0)
|
106
|
+
os (0.9.6)
|
107
|
+
public_suffix (3.0.1)
|
108
|
+
rack (2.0.3)
|
109
|
+
rack-test (0.8.2)
|
110
|
+
rack (>= 1.0, < 3)
|
111
|
+
rails (5.1.4)
|
112
|
+
actioncable (= 5.1.4)
|
113
|
+
actionmailer (= 5.1.4)
|
114
|
+
actionpack (= 5.1.4)
|
115
|
+
actionview (= 5.1.4)
|
116
|
+
activejob (= 5.1.4)
|
117
|
+
activemodel (= 5.1.4)
|
118
|
+
activerecord (= 5.1.4)
|
119
|
+
activesupport (= 5.1.4)
|
120
|
+
bundler (>= 1.3.0)
|
121
|
+
railties (= 5.1.4)
|
122
|
+
sprockets-rails (>= 2.0.0)
|
123
|
+
rails-dom-testing (2.0.3)
|
124
|
+
activesupport (>= 4.2.0)
|
125
|
+
nokogiri (>= 1.6)
|
126
|
+
rails-html-sanitizer (1.0.3)
|
127
|
+
loofah (~> 2.0)
|
128
|
+
railties (5.1.4)
|
129
|
+
actionpack (= 5.1.4)
|
130
|
+
activesupport (= 5.1.4)
|
131
|
+
method_source
|
132
|
+
rake (>= 0.8.7)
|
133
|
+
thor (>= 0.18.1, < 2.0)
|
134
|
+
rake (10.5.0)
|
135
|
+
rb-fsevent (0.10.2)
|
136
|
+
rb-inotify (0.9.10)
|
137
|
+
ffi (>= 0.5.0, < 2)
|
138
|
+
ruby_dep (1.5.0)
|
139
|
+
signet (0.8.1)
|
140
|
+
addressable (~> 2.3)
|
141
|
+
faraday (~> 0.9)
|
142
|
+
jwt (>= 1.5, < 3.0)
|
143
|
+
multi_json (~> 1.10)
|
144
|
+
sprockets (3.7.1)
|
145
|
+
concurrent-ruby (~> 1.0)
|
146
|
+
rack (> 1, < 3)
|
147
|
+
sprockets-rails (3.2.1)
|
148
|
+
actionpack (>= 4.0)
|
149
|
+
activesupport (>= 4.0)
|
150
|
+
sprockets (>= 3.0.0)
|
151
|
+
sqlite3 (1.3.13)
|
152
|
+
thor (0.20.0)
|
153
|
+
thread_safe (0.3.6)
|
154
|
+
tzinfo (1.2.4)
|
155
|
+
thread_safe (~> 0.1)
|
156
|
+
websocket-driver (0.6.5)
|
157
|
+
websocket-extensions (>= 0.1.0)
|
158
|
+
websocket-extensions (0.1.3)
|
159
|
+
|
160
|
+
PLATFORMS
|
161
|
+
ruby
|
162
|
+
|
163
|
+
DEPENDENCIES
|
164
|
+
bundler (~> 1.15)
|
165
|
+
grpc (~> 1.7.3)
|
166
|
+
grpc_for_rails!
|
167
|
+
jbuilder (~> 2.5)
|
168
|
+
listen (>= 3.0.5, < 3.2)
|
169
|
+
minitest (~> 5.0)
|
170
|
+
rails (~> 5.1.4)
|
171
|
+
rake (~> 10.0)
|
172
|
+
sqlite3
|
173
|
+
|
174
|
+
BUNDLED WITH
|
175
|
+
1.15.4
|
data/LICENSE.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2017 Vincent
|
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,68 @@
|
|
1
|
+
# GrpcForRails
|
2
|
+
|
3
|
+
Make grpc server and client easy for rails project.
|
4
|
+
|
5
|
+
grpc_for_rails provides a useful grpc_server wrapper and some flexible generators and rake tasks for rails project.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'grpc_for_rails'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install grpc_for_rails
|
22
|
+
|
23
|
+
Run grpc:install generator after install gem:
|
24
|
+
|
25
|
+
$ bin/rails g grpc:install
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Start grpc_server and daemonize it:
|
30
|
+
|
31
|
+
$ bundle exec grpc_server -p grpc_server.pid -d
|
32
|
+
|
33
|
+
Stop grpc_server:
|
34
|
+
|
35
|
+
$ kill -TERM `cat grpc_server.pid`
|
36
|
+
|
37
|
+
Generate grpc_server configuration scaffold:
|
38
|
+
|
39
|
+
$ bin/rails g grpc:install
|
40
|
+
|
41
|
+
It will generate folder `<rails root>/app/grpc` and sub-folder [`base`, `protos`, `services`] which are the default location for `grpc_for_rails`
|
42
|
+
|
43
|
+
And it will generate file `<rails root>/config/grpc_server.yml` which includes all grpc_server configurations `grpc_for_rails` supported. You can change it for your need.
|
44
|
+
|
45
|
+
Generate grpc_server service scaffold (TODO):
|
46
|
+
|
47
|
+
$ bin/rails g grpc:service <model name>
|
48
|
+
|
49
|
+
|
50
|
+
Generate grpc_server services ruby codes for grpc proto files:
|
51
|
+
|
52
|
+
$ bin/rails g grpc:base
|
53
|
+
|
54
|
+
It will generate ruby codes based on grpc proto files in `<rails root>/app/grpc/protos`.
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
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.
|
59
|
+
|
60
|
+
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).
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/xiewenwei/grpc_for_rails. 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.
|
65
|
+
|
66
|
+
## Code of Conduct
|
67
|
+
|
68
|
+
Everyone interacting in the GrpcForRails project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/xiewenwei/grpc_for_rails/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "grpc_for_rails"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/grpc_server
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'optparse'
|
3
|
+
require 'yaml'
|
4
|
+
require 'grpc_for_rails'
|
5
|
+
require 'active_support/core_ext/hash'
|
6
|
+
|
7
|
+
default_host_port = {
|
8
|
+
host: '0.0.0.0',
|
9
|
+
port: 3011,
|
10
|
+
pool_size: 5
|
11
|
+
}
|
12
|
+
|
13
|
+
options = {
|
14
|
+
env: 'development',
|
15
|
+
config: 'config/grpc_server.yml'
|
16
|
+
}
|
17
|
+
|
18
|
+
OptionParser.new do |opts|
|
19
|
+
opts.banner = "Usage: grpc_server [options]"
|
20
|
+
|
21
|
+
opts.separator "GRPC options:"
|
22
|
+
opts.on('-h', '--host HOST', 'Host (default 0.0.0.0)') do |value|
|
23
|
+
options[:host] = value
|
24
|
+
end
|
25
|
+
opts.on('-t', '--port PORT', 'Port (default: 3011)') do |value|
|
26
|
+
options[:port] = value
|
27
|
+
end
|
28
|
+
opts.on('-e', '--environment RAILS_ENV', 'Rails env (default: development)') do |value|
|
29
|
+
options[:env] = value
|
30
|
+
end
|
31
|
+
opts.on('-c', '--config CONFIG_FILE', 'Config file (default: config/grpc_server.yml)') do |value|
|
32
|
+
options[:config] = value
|
33
|
+
end
|
34
|
+
opts.on('-s', '--size pool_size', 'pool size for grpc_server (default: 5)') do
|
35
|
+
options[:pool_size] = value.to_i
|
36
|
+
end
|
37
|
+
opts.separator ""
|
38
|
+
|
39
|
+
opts.separator "Process options:"
|
40
|
+
opts.on("-d", "--daemonize", 'run daemonized in the background (default: false)') do
|
41
|
+
options[:daemonize] = true
|
42
|
+
end
|
43
|
+
opts.on("-p", "--pid PIDFILE", 'the pid filename') do |value|
|
44
|
+
options[:pidfile] = value
|
45
|
+
end
|
46
|
+
opts.on("-l", "--log LOGFILE", 'the log filename') do |value|
|
47
|
+
options[:logfile] = value
|
48
|
+
end
|
49
|
+
opts.separator ""
|
50
|
+
end.parse!
|
51
|
+
|
52
|
+
if File.exists? options[:config]
|
53
|
+
config = YAML.load(File.read(options[:config]))
|
54
|
+
options = config[options[:env]].symbolize_keys.merge(options)
|
55
|
+
end
|
56
|
+
|
57
|
+
options = default_host_port.merge(options)
|
58
|
+
|
59
|
+
puts "Start grpc_server with:\n #{options}"
|
60
|
+
GrpcForRails::Server.new(options).run
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "grpc_for_rails/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "grpc_for_rails"
|
8
|
+
spec.version = GrpcForRails::VERSION
|
9
|
+
spec.authors = ["Vincent"]
|
10
|
+
spec.email = ["xiewenwei@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Make grpc server and client easy for rails project.}
|
13
|
+
spec.description = %q{grpc_for_rails provides a useful grpc_server wrapper and some flexible generators and rake tasks for rails project.}
|
14
|
+
spec.homepage = "https://github.com/xiewenwei/grpc_for_rails"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency 'railties', '>= 4.2.0'
|
25
|
+
spec.add_dependency "grpc", "~> 1.2"
|
26
|
+
spec.add_dependency 'grpc-tools', '~> 1.2'
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
31
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
|
3
|
+
module GrpcForRails
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer 'grpc_for_rails.initialization' do |app|
|
6
|
+
$LOAD_PATH.unshift "#{app.root}/app/grpc/base"
|
7
|
+
$LOAD_PATH.unshift "#{app.root}/app/grpc/services"
|
8
|
+
end
|
9
|
+
|
10
|
+
rake_tasks do
|
11
|
+
current_path = File.expand_path('..', __FILE__)
|
12
|
+
Dir["#{current_path}/../tasks/*.rake"].each { |f| load f }
|
13
|
+
end
|
14
|
+
|
15
|
+
generators do
|
16
|
+
current_path = File.expand_path('..', __FILE__)
|
17
|
+
Dir["#{current_path}/../rails/generators/*.rb"].each { |f| require_relative f }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require 'grpc'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module GRPC
|
5
|
+
def self.logger
|
6
|
+
@logger ||= Logger.new($stdout)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module GrpcForRails
|
11
|
+
class Server
|
12
|
+
attr_reader :options
|
13
|
+
|
14
|
+
EXECUTABLES = ['bin/rails', 'script/rails']
|
15
|
+
|
16
|
+
def initialize(options)
|
17
|
+
@options = options
|
18
|
+
ENV['RAILS_ENV'] = @options[:env]
|
19
|
+
|
20
|
+
# daemonization will change CWD so expand relative paths now
|
21
|
+
options[:logfile] = File.expand_path(logfile) if logfile?
|
22
|
+
options[:pidfile] = File.expand_path(pidfile) if pidfile?
|
23
|
+
end
|
24
|
+
|
25
|
+
def run
|
26
|
+
check_pid
|
27
|
+
daemonize if daemonize?
|
28
|
+
write_pid
|
29
|
+
|
30
|
+
if logfile?
|
31
|
+
redirect_output
|
32
|
+
elsif daemonize?
|
33
|
+
suppress_output
|
34
|
+
end
|
35
|
+
startup
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def startup
|
41
|
+
load_rails_environment!
|
42
|
+
|
43
|
+
@server ||= GRPC::RpcServer.new pool_size: @options[:pool_size]
|
44
|
+
@server.add_http2_port "#{@options[:host]}:#{@options[:port]}", :this_port_is_insecure
|
45
|
+
services.each { |service| @server.handle service }
|
46
|
+
begin
|
47
|
+
@server.run
|
48
|
+
rescue SignalException => e
|
49
|
+
GRPC.logger.info "shutdown server..."
|
50
|
+
@server.stop
|
51
|
+
force_delete_pidfile!
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def daemonize?
|
56
|
+
options[:daemonize]
|
57
|
+
end
|
58
|
+
|
59
|
+
def logfile
|
60
|
+
options[:logfile]
|
61
|
+
end
|
62
|
+
|
63
|
+
def pidfile
|
64
|
+
options[:pidfile]
|
65
|
+
end
|
66
|
+
|
67
|
+
def logfile?
|
68
|
+
!logfile.nil?
|
69
|
+
end
|
70
|
+
|
71
|
+
def pidfile?
|
72
|
+
!pidfile.nil?
|
73
|
+
end
|
74
|
+
|
75
|
+
def write_pid
|
76
|
+
if pidfile?
|
77
|
+
begin
|
78
|
+
File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY){|f| f.write("#{Process.pid}") }
|
79
|
+
at_exit { File.delete(pidfile) if File.exists?(pidfile) }
|
80
|
+
rescue Errno::EEXIST
|
81
|
+
check_pid
|
82
|
+
retry
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def check_pid
|
88
|
+
if pidfile?
|
89
|
+
case pid_status(pidfile)
|
90
|
+
when :running, :not_owned
|
91
|
+
puts "A server is already running. Check #{pidfile}"
|
92
|
+
exit(1)
|
93
|
+
when :dead
|
94
|
+
File.delete(pidfile)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def force_delete_pidfile!
|
100
|
+
if pidfile?
|
101
|
+
File.delete(pidfile) rescue nil
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def pid_status(pidfile)
|
106
|
+
return :exited unless File.exists?(pidfile)
|
107
|
+
pid = ::File.read(pidfile).to_i
|
108
|
+
return :dead if pid == 0
|
109
|
+
Process.kill(0, pid) # check process status
|
110
|
+
:running
|
111
|
+
rescue Errno::ESRCH
|
112
|
+
:dead
|
113
|
+
rescue Errno::EPERM
|
114
|
+
:not_owned
|
115
|
+
end
|
116
|
+
|
117
|
+
def daemonize
|
118
|
+
exit if fork
|
119
|
+
Process.setsid
|
120
|
+
exit if fork
|
121
|
+
#Dir.chdir "/"
|
122
|
+
end
|
123
|
+
|
124
|
+
def redirect_output
|
125
|
+
FileUtils.mkdir_p(File.dirname(logfile), mode: 0755)
|
126
|
+
FileUtils.touch logfile
|
127
|
+
File.chmod(0644, logfile)
|
128
|
+
$stderr.reopen(logfile, 'a')
|
129
|
+
$stdout.reopen($stderr)
|
130
|
+
$stdout.sync = $stderr.sync = true
|
131
|
+
end
|
132
|
+
|
133
|
+
def suppress_output
|
134
|
+
$stderr.reopen('/dev/null', 'a')
|
135
|
+
$stdout.reopen($stderr)
|
136
|
+
end
|
137
|
+
|
138
|
+
def services
|
139
|
+
Dir["#{Rails.root}/app/grpc/services/**/*.rb"].each { |f| require f }
|
140
|
+
|
141
|
+
result = Dir["#{Rails.root}/app/grpc/services/*.rb"].map do |file|
|
142
|
+
class_name = file.split('/').last.sub(/\.rb$/, '').camelize
|
143
|
+
Object.const_get class_name
|
144
|
+
end
|
145
|
+
result.delete_if { |e| !(e < GRPC::GenericService) }
|
146
|
+
result
|
147
|
+
end
|
148
|
+
|
149
|
+
def load_rails_environment!
|
150
|
+
loop do
|
151
|
+
if exe = find_executable
|
152
|
+
Object.const_set(:APP_PATH, File.expand_path('config/application', Dir.pwd))
|
153
|
+
require File.expand_path('../environment', APP_PATH)
|
154
|
+
break
|
155
|
+
end
|
156
|
+
|
157
|
+
raise 'You are not in Rails project' if Pathname.new(Dir.pwd).root?
|
158
|
+
|
159
|
+
# Otherwise keep moving upwards in search of an executable.
|
160
|
+
Dir.chdir('..')
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def find_executable
|
165
|
+
EXECUTABLES.find { |exe| File.file?(exe) }
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
|
3
|
+
module Grpc
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path '../templates', __FILE__
|
6
|
+
desc "Install grpc_server.yml file to your application."
|
7
|
+
|
8
|
+
def mkdirs
|
9
|
+
empty_directory 'app/grpc/services'
|
10
|
+
empty_directory 'app/grpc/base'
|
11
|
+
empty_directory 'app/grpc/protos'
|
12
|
+
end
|
13
|
+
|
14
|
+
def copy_config
|
15
|
+
template 'grpc_server.yml', 'config/grpc_server.yml'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
default: &default
|
2
|
+
host: 0.0.0.0
|
3
|
+
port: 3011
|
4
|
+
pidfile: tmp/pids/grpc_server.pid
|
5
|
+
logfile: log/grpc_server.log
|
6
|
+
pool_size: 5
|
7
|
+
daemonize: false
|
8
|
+
|
9
|
+
development:
|
10
|
+
host: 127.0.0.1
|
11
|
+
<<: *default
|
12
|
+
|
13
|
+
test:
|
14
|
+
<<: *default
|
15
|
+
|
16
|
+
production:
|
17
|
+
<<: *default
|
data/lib/tasks/grpc.rake
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
namespace :grpc do
|
2
|
+
desc 'Parse protos files defined in app/grpc/protos to output ruby codes.'
|
3
|
+
task base: :environment do
|
4
|
+
root_dir = Rails.root
|
5
|
+
exec "grpc_tools_ruby_protoc -I #{root_dir}/app/grpc/protos --ruby_out=#{root_dir}/app/grpc/base --grpc_out=#{root_dir}/app/grpc/base #{root_dir}/app/grpc/protos/*.proto"
|
6
|
+
end
|
7
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grpc_for_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vincent
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: grpc
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: grpc-tools
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.15'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.15'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.0'
|
97
|
+
description: grpc_for_rails provides a useful grpc_server wrapper and some flexible
|
98
|
+
generators and rake tasks for rails project.
|
99
|
+
email:
|
100
|
+
- xiewenwei@gmail.com
|
101
|
+
executables:
|
102
|
+
- grpc_server
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- ".gitignore"
|
107
|
+
- ".ruby-version"
|
108
|
+
- ".travis.yml"
|
109
|
+
- CODE_OF_CONDUCT.md
|
110
|
+
- Gemfile
|
111
|
+
- Gemfile.lock
|
112
|
+
- LICENSE.md
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- exe/grpc_server
|
118
|
+
- grpc_for_rails.gemspec
|
119
|
+
- lib/grpc_for_rails.rb
|
120
|
+
- lib/grpc_for_rails/railtie.rb
|
121
|
+
- lib/grpc_for_rails/server.rb
|
122
|
+
- lib/grpc_for_rails/version.rb
|
123
|
+
- lib/rails/generators/install_generator.rb
|
124
|
+
- lib/rails/generators/templates/grpc_server.yml
|
125
|
+
- lib/tasks/grpc.rake
|
126
|
+
homepage: https://github.com/xiewenwei/grpc_for_rails
|
127
|
+
licenses:
|
128
|
+
- MIT
|
129
|
+
metadata: {}
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements: []
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 2.6.11
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: Make grpc server and client easy for rails project.
|
150
|
+
test_files: []
|