jrpc-gateway

module
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2024 License: Apache-2.0

README

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

Directories

Path Synopsis
Package jrpc_doc is a protoc plugin for generating documentation from your proto files.
Package jrpc_doc is a protoc plugin for generating documentation from your proto files.
cmd/protoc-gen-jrpc-doc
protoc-gen-jrpc-doc is used to generate documentation from comments in your proto files.
protoc-gen-jrpc-doc is used to generate documentation from comments in your proto files.
extensions
Package extensions implements a system for working with extended options.
Package extensions implements a system for working with extended options.
tmp/googleapis/google/example/endpointsapis/goapp
The goapp command implements a simple App Engine app to demonstrate how to use the Service Control API v2 for admission control.
The goapp command implements a simple App Engine app to demonstrate how to use the Service Control API v2 for admission control.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL