Skip to content

Commit

Permalink
feat: use forwardResponseOption for gateway HTTP redirection (#22)
Browse files Browse the repository at this point in the history
* feat: use forwardResponseOption for gateway HTTP redirection

* fix: duplicate imports and add TODO comment

Co-authored-by: alok <aloknerurkar@no-reply.com>
  • Loading branch information
aloknerurkar and alok committed Jan 2, 2022
1 parent 32a2742 commit 668fe3e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/slok/go-http-metrics v0.9.0
go.uber.org/fx v1.16.0
google.golang.org/grpc v1.42.0
google.golang.org/protobuf v1.27.1
)

require (
Expand Down Expand Up @@ -203,7 +204,6 @@ require (
golang.org/x/tools v0.1.7 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
lukechampine.com/blake3 v1.1.7 // indirect
Expand Down
17 changes: 16 additions & 1 deletion modules/node/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/plexsysio/go-msuite/modules/diag/status"
"github.com/plexsysio/go-msuite/utils"
"go.uber.org/fx"
"google.golang.org/protobuf/proto"
)

var log = logger.Logger("http")
Expand Down Expand Up @@ -42,8 +43,22 @@ func NewHTTPServerMux() *nhttp.ServeMux {
return nhttp.NewServeMux()
}

// HTTP redirect handler for gRPC gateway methods
// TODO: maybe provide a way to configure these options using main Options
func responseHeaderMatcher(_ context.Context, w nhttp.ResponseWriter, _ proto.Message) error {
headers := w.Header()
if location, ok := headers["Grpc-Metadata-Location"]; ok {
w.Header().Set("Location", location[0])
w.WriteHeader(nhttp.StatusFound)
}

return nil
}

func NewGRPCGateway() *runtime.ServeMux {
return runtime.NewServeMux()
return runtime.NewServeMux(
runtime.WithForwardResponseOption(responseHeaderMatcher),
)
}

type httpReporter struct {
Expand Down

0 comments on commit 668fe3e

Please sign in to comment.