Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
yugui committed Apr 2, 2015
1 parent 1480f4e commit d171440
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
GO_PLUGIN=bin/protoc-gen-go
GO_PLUGIN_PKG=github.com/golang/protobuf/protoc-gen-go
GATEWAY_PLUGIN=bin/protoc-gen-grpc-gateway
GATEWAY_PLUGIN_PKG=github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway
GATEWAY_PLUGIN_SRC=protoc-gen-grpc-gateway/main.go protoc-gen-grpc-gateway/generator.go
OPTIONS_GO=options/options.pb.go
OPTIONS_PROTO=options/options.proto
PKGMAP=Mgoogle/protobuf/descriptor.proto=$(GO_PLUGIN_PKG)/descriptor
EXAMPLES=examples/echo_service.proto

generate: $(OPTIONS_GO)

Expand All @@ -15,5 +19,12 @@ $(GO_PLUGIN):
$(OPTIONS_GO): $(OPTIONS_PROTO) $(GO_PLUGIN)
protoc -I $(dir $(shell which protoc))/../include -I. --plugin=$(GO_PLUGIN) --go_out=$(PKGMAP):. $(OPTIONS_PROTO)

$(GATEWAY_PLUGIN): $(OPTIONS_GO) $(GATEWAY_PLUGIN_SRC)
go build -o $@ $(GATEWAY_PLUGIN_PKG)

test: $(GO_PLUGIN) $(GATEWAY_PLUGIN) $(EXAMPLES)
protoc -I $(dir $(shell which protoc))/../include -I. --plugin=$(GO_PLUGIN) --go_out=$(PKGMAP),plugins=grpc:. $(EXAMPLES)
protoc -I $(dir $(shell which protoc))/../include -I. --plugin=$(GATEWAY_PLUGIN) --grpc-gateway_out=logtostderr=true:. $(EXAMPLES)

realcelan:
rm -f $(OPTIONS_GO)
1 change: 1 addition & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/protoc-gen-go
/protoc-gen-grpc-gateway
98 changes: 98 additions & 0 deletions examples/echo_service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 96 additions & 0 deletions examples/echo_service.pb.gw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package examples

import (
"encoding/json"
"net/http"

"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"
)

func handle_EchoService_Echo(ctx context.Context, c *EchoServiceClient, req *http.Request) (proto.Message, error) {
protoReq := new(SimpleMessage)

if err = json.NewDecoder(req.Body).Decode(&protoReq); err != nil {
return err
}

return c.Echo(ctx, protoReq)
}

var (
endpointEchoService string
)

func init() {

flag.StringVar(&endpointEchoService, "echo_service_endpoint", "", "endpoint host:port of EchoService")

}

type handler struct {
mux http.ServeMux
conns map[string]*grpc.ClientConn
}

func (h *handler) Close() error {
var err error
for svc, conn := range h.conns {
cerr := conn.Close()
if err == nil {
err = cerr
}
if cerr != nil {
glog.Errorf("Failed to close gRPC connection to %s: %v", svc, err)
}
}
return err
}

func NewHandler(ctx context.Context) (http.Handler, error) {
h := &handler{
conn: make(map[string]*grpc.ClientConn),
}
var err error
defer func() {
if err != nil {
h.Close()
}
}()

err = func() error {
conn, err := grpc.Dial(endpointEchoService)
if err != nil {
return err
}
h.conn["EchoService"] = conn
client := NewEchoServiceClient(conn)

mux.HandleFunc("/v1/example/echo", func(w http.ResponseWriter, req *http.Request) {
resp, err := handle_EchoService_Echo(ctx, client, req)
if err != nil {
glog.Errorf("RPC error: %v", err)
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
buf, err := proto.Marshal(resp)
if err != nil {
glog.Errorf("Marshal error: %v", err)
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err = w.Write(buf); err != nil {
glog.Errorf("Failed to write response: %v", err)
}
})

}()
if err != nil {
return nil, err
}

return h, nil
}
18 changes: 18 additions & 0 deletions examples/echo_service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";
option go_package = "examples";
package gengo.grpc.gateway.example;

import "options/options.proto";

message SimpleMessage {
string id = 1;
}

service EchoService {
rpc Echo(SimpleMessage) returns (SimpleMessage) {
option (gengo.grpc.gateway.ApiMethodOptions.api_options) = {
path: "/v1/example/echo"
method: "POST"
};
}
}

0 comments on commit d171440

Please sign in to comment.