Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use forwardResponseOption for gateway HTTP redirection #22

Merged
merged 2 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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