protoc-gen-grpc-transcode

command module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

README

protoc-gen-grpc-transcode

protoc-gen-grpc-transcode is a protoc plugin for generating a simple HTTP POST+json transcoder for grpc in Go.

The generated code does not depend on any packages in this repo.

It is meant to be used when along side of or instead of grpc-gateway when you need simple json to grpc transcoding.

Usage

Install the plugin:

go install github.com/bakins/protoc-gen-grpc-transcode@latest

Generate the server code (using example in internal/example):

protoc -I . \
    --go_out=. \
    --go_opt=paths=source_relative \
    --go-grpc_out=. \
    --go-grpc_opt=paths=source_relative \
    --grpc-transcode_out=. \
    --grpc-transcode_opt=paths=source_relative \
    *.proto

Now use in your program

package main

import (
	"log"
	"net/http"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"

	pb "github.com/bakins/protoc-gen-grpc-transcode/internal/example"
)

func main() {
	conn, err := grpc.Dial("127.0.0.1:5000", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatal(err)
	}

	transcoder := pb.NewGreeterTranscode(conn)

	mux := http.NewServeMux()

	// Methods returns a list of methods in the form
	// /<package>.<service>/<method> that is suitable
	// for using in a mux
	for _, method := range transcoder.Methods() {
		mux.Handle(method, transcoder)
	}

	http.ListenAndServe(":8080", mux)
}

The test from another terminal:

$ curl -H "Content-Type: application/json" --data-binary '{"name": "world" }' http://127.0.0.1:8080/helloworld.Greeter/SayHello

{"message": "hello world"}

LICENSE

See LICENSE

TODO

  • more test cases

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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