Skip to content

Commit

Permalink
Make the forwarder function customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
yugui committed Jul 13, 2015
1 parent d430203 commit d45f379
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
46 changes: 35 additions & 11 deletions examples/a_bit_of_everything.pb.gw.go

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

10 changes: 8 additions & 2 deletions examples/echo_service.pb.gw.go

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

12 changes: 10 additions & 2 deletions protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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}}`))
)
6 changes: 3 additions & 3 deletions runtime/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d45f379

Please sign in to comment.