jrpc-gateway is bridge between json-rpc 2.0 and gRPC
This repository consists of:
- jrpc package
- protoc-gen-jrpc-gateway protoc plugin
- protoc-gen-jrpc-doc protoc plugin
go get github.com/pacviewer/jrpc-gateway/jrpc
protoc-gen-jrpc-gateway generates json-rpc to grpc bridge code based on proto files
go install github.com/pacviewer/jrpc-gateway/[email protected]
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;
}
mkdir gen
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
go mod tidy
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)
}
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
{"jsonrpc":"2.0","id":"1111","result":{"message":"Hello john"}}
go install github.com/pacviewer/jrpc-gateway/protoc-gen-jrpc-doc/cmd/[email protected]
protoc --jrpc-doc_out=gen --jrpc-doc_opt=./json-rpc-md.tmpl,json-rpc.md \
greeting.proto