Skip to content

PacViewer/jrpc-gateway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 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.3.2

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)  
    }
    
    greetingService := pb.NewGreetingServiceJsonRPC(grpcConn)
    
    server := jrpc.NewServer()
    server.RegisterServices(greetingService)
    
    // json-rpc listener
    lis, err = net.Listen("tcp", "localhost:8687")
    if err != nil {
      log.Fatalln(err)
    }
    
    if err := jgw.Serve(lis); err != nil {
      log.Fatalln(err)
    }

Test method call

Request:

curl -X POST -H 'Content-Type: application/json' \
     -H 'Authorization: Basic <base64-encoded-credentials>' \
     -H 'Custom-Header: CustomValue' \
     -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