From d45f379005f17fbfdf5e8196da484da684a876a0 Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Mon, 13 Jul 2015 16:15:16 +0900 Subject: [PATCH] Make the forwarder function customizable --- examples/a_bit_of_everything.pb.gw.go | 46 ++++++++++++++----- examples/echo_service.pb.gw.go | 10 +++- .../gengateway/template.go | 12 ++++- runtime/handler.go | 6 +-- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/examples/a_bit_of_everything.pb.gw.go b/examples/a_bit_of_everything.pb.gw.go index 3c009a51093..8034dcbd46d 100644 --- a/examples/a_bit_of_everything.pb.gw.go +++ b/examples/a_bit_of_everything.pb.gw.go @@ -384,7 +384,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_ABitOfEverythingService_Create_0(ctx, w, req, resp) }) @@ -395,7 +395,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_ABitOfEverythingService_CreateBody_0(ctx, w, req, resp) }) @@ -406,7 +406,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_ABitOfEverythingService_BulkCreate_0(ctx, w, req, resp) }) @@ -417,7 +417,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_ABitOfEverythingService_Lookup_0(ctx, w, req, resp) }) @@ -428,7 +428,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseStream(w, func() (proto.Message, error) { return resp.Recv() }) + forward_ABitOfEverythingService_List_0(ctx, w, req, func() (proto.Message, error) { return resp.Recv() }) }) @@ -439,7 +439,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_ABitOfEverythingService_Update_0(ctx, w, req, resp) }) @@ -450,7 +450,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_ABitOfEverythingService_Delete_0(ctx, w, req, resp) }) @@ -461,7 +461,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_ABitOfEverythingService_Echo_0(ctx, w, req, resp) }) @@ -472,7 +472,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_ABitOfEverythingService_Echo_1(ctx, w, req, resp) }) @@ -483,7 +483,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_ABitOfEverythingService_Echo_2(ctx, w, req, resp) }) @@ -494,7 +494,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se return } - runtime.ForwardResponseStream(w, func() (proto.Message, error) { return resp.Recv() }) + forward_ABitOfEverythingService_BulkEcho_0(ctx, w, req, func() (proto.Message, error) { return resp.Recv() }) }) @@ -524,3 +524,27 @@ var ( pattern_ABitOfEverythingService_BulkEcho_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"v1", "example", "a_bit_of_everything", "echo"}, "")) ) + +var ( + forward_ABitOfEverythingService_Create_0 = runtime.ForwardResponseMessage + + forward_ABitOfEverythingService_CreateBody_0 = runtime.ForwardResponseMessage + + forward_ABitOfEverythingService_BulkCreate_0 = runtime.ForwardResponseMessage + + forward_ABitOfEverythingService_Lookup_0 = runtime.ForwardResponseMessage + + forward_ABitOfEverythingService_List_0 = runtime.ForwardResponseStream + + forward_ABitOfEverythingService_Update_0 = runtime.ForwardResponseMessage + + forward_ABitOfEverythingService_Delete_0 = runtime.ForwardResponseMessage + + forward_ABitOfEverythingService_Echo_0 = runtime.ForwardResponseMessage + + forward_ABitOfEverythingService_Echo_1 = runtime.ForwardResponseMessage + + forward_ABitOfEverythingService_Echo_2 = runtime.ForwardResponseMessage + + forward_ABitOfEverythingService_BulkEcho_0 = runtime.ForwardResponseStream +) diff --git a/examples/echo_service.pb.gw.go b/examples/echo_service.pb.gw.go index 23835caae61..5f1561803f2 100644 --- a/examples/echo_service.pb.gw.go +++ b/examples/echo_service.pb.gw.go @@ -94,7 +94,7 @@ func RegisterEchoServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_EchoService_Echo_0(ctx, w, req, resp) }) @@ -105,7 +105,7 @@ func RegisterEchoServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn return } - runtime.ForwardResponseMessage(ctx, w, resp) + forward_EchoService_EchoBody_0(ctx, w, req, resp) }) @@ -117,3 +117,9 @@ var ( pattern_EchoService_EchoBody_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"v1", "example", "echo_body"}, "")) ) + +var ( + forward_EchoService_Echo_0 = runtime.ForwardResponseMessage + + forward_EchoService_EchoBody_0 = runtime.ForwardResponseMessage +) diff --git a/protoc-gen-grpc-gateway/gengateway/template.go b/protoc-gen-grpc-gateway/gengateway/template.go index 2820648da93..22905c8bd74 100644 --- a/protoc-gen-grpc-gateway/gengateway/template.go +++ b/protoc-gen-grpc-gateway/gengateway/template.go @@ -228,9 +228,9 @@ func Register{{$svc.GetName}}Handler(ctx context.Context, mux *runtime.ServeMux, return } {{if $m.GetServerStreaming}} - runtime.ForwardResponseStream(w, func() (proto.Message, error) { return resp.Recv() }) + forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(ctx, w, req, func() (proto.Message, error) { return resp.Recv() }) {{else}} - runtime.ForwardResponseMessage(ctx, w, resp) + forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(ctx, w, req, resp) {{end}} }) {{end}} @@ -245,5 +245,13 @@ var ( {{end}} {{end}} ) + +var ( + {{range $m := $svc.Methods}} + {{range $b := $m.Bindings}} + forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}} = {{if $m.GetServerStreaming}}runtime.ForwardResponseStream{{else}}runtime.ForwardResponseMessage{{end}} + {{end}} + {{end}} +) {{end}}`)) ) diff --git a/runtime/handler.go b/runtime/handler.go index eba21bd6032..63eb04e4650 100644 --- a/runtime/handler.go +++ b/runtime/handler.go @@ -17,7 +17,7 @@ type responseStreamChunk struct { } // ForwardResponseStream forwards the stream from gRPC server to REST client. -func ForwardResponseStream(w http.ResponseWriter, recv func() (proto.Message, error)) { +func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http.Request, recv func() (proto.Message, error)) { f, ok := w.(http.Flusher) if !ok { glog.Errorf("Flush not supported in %T", w) @@ -58,8 +58,8 @@ func ForwardResponseStream(w http.ResponseWriter, recv func() (proto.Message, er } } -// ForwardResponseMessage forwards the message from gRPC server to REST client. -func ForwardResponseMessage(ctx context.Context, w http.ResponseWriter, resp proto.Message) { +// ForwardResponseMessage forwards the message "resp" from gRPC server to REST client. +func ForwardResponseMessage(ctx context.Context, w http.ResponseWriter, req *http.Request, resp proto.Message) { buf, err := json.Marshal(resp) if err != nil { glog.Errorf("Marshal error: %v", err)