Skip to content

Commit c323909

Browse files
jacksontjachew22
authored andcommitted
Add Handler method to pass in a client (#454)
* Add Handler method to pass in a client Right now if you want to embed the JSON proxy into your service you end up having to have a client talking to loopback which is messy. With this API addition you can simply pass in something which implements the client interface -- meaning you could wrap it yourself and return directly (without all the loopback and serialization overhead). Note that this does not apply interceptors and other gRPC niceties. Please ensure your application doesn't need interceptors before using this method.
1 parent ac41185 commit c323909

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

examples/examplepb/a_bit_of_everything.pb.gw.go

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/examplepb/echo_service.pb.gw.go

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/examplepb/flow_combination.pb.gw.go

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/examplepb/stream.pb.gw.go

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protoc-gen-grpc-gateway/gengateway/template.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,15 @@ func Register{{$svc.GetName}}HandlerFromEndpoint(ctx context.Context, mux *runti
338338
// Register{{$svc.GetName}}Handler registers the http handlers for service {{$svc.GetName}} to "mux".
339339
// The handlers forward requests to the grpc endpoint over "conn".
340340
func Register{{$svc.GetName}}Handler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error {
341-
client := New{{$svc.GetName}}Client(conn)
341+
return Register{{$svc.GetName}}HandlerClient(ctx, mux, New{{$svc.GetName}}Client(conn))
342+
}
343+
344+
// Register{{$svc.GetName}}Handler registers the http handlers for service {{$svc.GetName}} to "mux".
345+
// The handlers forward requests to the grpc endpoint over the given implementation of "{{$svc.GetName}}Client".
346+
// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "{{$svc.GetName}}Client"
347+
// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in
348+
// "{{$svc.GetName}}Client" to call the correct interceptors.
349+
func Register{{$svc.GetName}}HandlerClient(ctx context.Context, mux *runtime.ServeMux, client {{$svc.GetName}}Client) error {
342350
{{range $m := $svc.Methods}}
343351
{{range $b := $m.Bindings}}
344352
mux.Handle({{$b.HTTPMethod | printf "%q"}}, pattern_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {

0 commit comments

Comments
 (0)