forked from grpc-ecosystem/grpc-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/protoc-gen-go | ||
/protoc-gen-grpc-gateway |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}; | ||
} | ||
} |