Skip to content

Json-RPC transacoding to GRPC | Proto Compiler plugin

License

Notifications You must be signed in to change notification settings

alidevjimmy/jrpc-gateway

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jrpc-gateway

jrpc-gateway is bridge between json-rpc 2.0 and gRPC

This repository consists of:

jrpc

installation

go get github.com/pacviewer/jrpc-gateway/jrpc

protoc-gen-jrpc-gateway

protoc-gen-jrpc-gateway generates json-rpc to grpc bridge code based on proto files

Installation

go install github.com/pacviewer/jrpc-gateway/protoc-gen-jrpc-gateway@v0.1.4

Example

greeting.proto

syntax = "proto3";
package greeting;
option go_package = "/greeting";

service GreetingService {
  rpc Greeting(GreetingReq) returns(GreetingResp) {}
}

message GreetingReq {
  string name = 1;
}

message GreetingResp {
  string message = 2;
}

Create gen directory

mkdir gen

Generate files

protoc --go_out=gen --go_opt=paths=source_relative \
    --go-grpc_out=gen --go-grpc_opt=paths=source_relative \
    --jrpc-gateway_out=gen \
    greeting.proto

these three files will be created for you in gen directory:

  • greeting_grpc.pb.go
  • greeting.pb.go
  • greeting_jgw.pb.go

Get dependencies

go mod tidy

Register JSON-RPC methods

grpcConn, err := grpc.DialContext(
context.Background(),
  "127.0.0.1:8686", // grpc server address
  grpc.WithTransportCredentials(insecure.NewCredentials()),
)

if err != nil {
  log.Fatalln(err)  
}

greeting := pb.NewGreetingServiceClient(grpcConn)
greetingService := pb.NewGreetingServiceJsonRpcService(greeting)

jgw := jrpc.NewServer()
jgw.RegisterServices(&greetingService)

// json-rpc listener
lis, err = net.Listen("tcp", "localhost:8687")
if err != nil {
  log.Fatalln(err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", jgw.HttpHandler)
server := &http.Server{
  Handler:           mux,
  Addr:              lis.Addr().String(),
  ReadHeaderTimeout: 3 * time.Second,
}

if err := server.Serve(lis); err != nil {
  log.Fatalln(err)
}

Test method call

Request:

curl -X POST -H 'Content-Type: application/json' \
     -d '{"jsonrpc":"2.0","id":"1111","method":"greeting.greeting_service.greeting", "params":{"name":"john"}}' \
     http://localhost:8687

Response:

{"jsonrpc":"2.0","id":"1111","result":{"message":"Hello john"}}

protoc-gen-jrpc-doc

Installation

go install github.com/pacviewer/jrpc-gateway/protoc-gen-jrpc-doc/cmd/protoc-gen-jrpc-doc@v0.1.4

Generate doc

protoc --jrpc-doc_out=gen --jrpc-doc_opt=./json-rpc-md.tmpl,json-rpc.md \
  greeting.proto

About

Json-RPC transacoding to GRPC | Proto Compiler plugin

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Starlark 96.8%
  • Go 1.6%
  • CMake 1.3%
  • Other 0.3%