Skip to content

Commit

Permalink
Updatessss
Browse files Browse the repository at this point in the history
  • Loading branch information
dorner committed Mar 3, 2024
1 parent 9a096f2 commit 7670dea
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/goreleaser.yml → .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@ on:
workflow_dispatch:

jobs:
test:
runs-on: [ubuntu-latest]
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.21'
- name: Install Protoc
uses: arduino/setup-protoc@v2
- run: git reset --hard
- run: git clean -f -d
- run: go test ./...

build_and_deploy:
needs: test
runs-on: [ubuntu-latest]
steps:
- name: Checkout code
Expand Down
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ and the generated controller will look like this:
require 'services/example/example_services_pb'
class ExampleServiceController < ActionController::Base
protect_from_forgery with: :null_session
METHOD_PARAM_MAP = {
rescue_from Google::Protobuf::TypeError do |e|
"example" => [
{name: "name", val: nil, split_name:["name"]},
],
}.freeze
rescue_from StandardError do |e|
render json: GrpcRest.error_msg(e)
end
Expand All @@ -119,6 +126,36 @@ Rails.application.routes.draw do
end
```

### Hooking up Callbacks

If you're using [gruf](https://github.com/bigcommerce/gruf), as long as your Gruf controllers are loaded on application load, you don't have to do anything else - grpc-rest will automatically hook the callbacks up. If you're not, you have to tell GrpcRest about your server. An example might look like this:

```ruby
# grpc_setup.rb, a shared library file somewhere in the app
def grpc_server
s = GRPC::RpcServer.new
s.handle(MyImpl.new) # handler inheriting from your service class - see https://grpc.io/docs/languages/ruby/basics/
s
end
# startup script for gRPC
require "grpc_setup"
server = grpc_server
server.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
# Rails initializer
require "grpc_setup"
server = grpc_server
GrpcRest.register_server(server)
```

## To Do

* Support repeated fields via comma-separation (matches grpc-gateway, but is it really useful?)
* Install via homebrew and/or have the binary in the gem itself

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/flipp-oss/grpc-rest.
Expand Down
48 changes: 34 additions & 14 deletions lib/grpc_rest.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
module GrpcRest
class << self

def register_server(server)
@server = server
end

def underscore(s)
GRPC::GenericService.underscore(s)
end

# Gets a sub record from a proto. If it doesn't exist, initialize it and set it to the proto,
# then return it.
def sub_field(proto, name)
Expand Down Expand Up @@ -67,25 +75,37 @@ def error_msg(error)
}
end

def send_gruf_request(klass, service_obj, method, request)
ref = service_obj.rpc_descs[method.classify.to_sym]
handler = klass.new(
method_key: method.to_sym,
service: service_obj,
rpc_desc: ref,
active_call: nil,
message: request
)
handler.send(method.to_sym)
end

def send_grpc_request(service, method, request)
server_parts = service.split('::')
service_name = (server_parts[..-2].map { |p| underscore(p)} + [server_parts[-1]]).join('.')
route = "/#{service_name}/#{method.classify}"
handler = @server.send(:rpc_handlers)[route.to_sym]
handler.call(request)
end

def send_request(service, method, request)
service_obj = service.constantize::Service
response = if defined?(Gruf)
if defined?(Gruf)
service_obj = service.constantize::Service
klass = ::Gruf::Controllers::Base.subclasses.find do |k|
k.bound_service == service_obj
end
ref = service_obj.rpc_descs[method.classify.to_sym]
handler = klass.new(
method_key: method.to_sym,
service: service_obj,
rpc_desc: ref,
active_call: nil,
message: request
)
handler.send(method.to_sym)
else
raise 'Non-gruf grpc not implemented yet!'
if klass
return send_gruf_request(klass, service_obj, method, request).to_h
end
end
response.to_h
send_grpc_request(service, method, request).to_h
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/grpc_rest/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GrpcRest
VERSION = "0.1.0"
VERSION = "0.1.1"
end
2 changes: 1 addition & 1 deletion protoc-gen-rails/internal/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ require 'services/geo_admin/v1/test_services_pb'
class {{.ControllerName}}Controller < ActionController::Base
protect_from_forgery with: :null_session
rescue_from Google::Protobuf::TypeError do |e|
rescue_from StandardError do |e|
render json: GrpcRest.error_msg(e)
end
METHOD_PARAM_MAP = {
Expand Down

0 comments on commit 7670dea

Please sign in to comment.