Skip to content

Commit

Permalink
Merge pull request #98 from agungcandra/change-path-with-pattern
Browse files Browse the repository at this point in the history
fix: change path params with pattern
  • Loading branch information
dongxuny authored Oct 24, 2023
2 parents f54bd55 + d3aef8c commit d4d0cc5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
44 changes: 26 additions & 18 deletions boot/gw_server_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/textproto"
"strings"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
rkmid "github.com/rookie-ninja/rk-entry/v2/middleware"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"net/http"
"net/textproto"
"strings"
)

// Gateway options for marshaller and unmarshaler.
Expand Down Expand Up @@ -165,27 +166,34 @@ func NewRkGwServerMuxOptions(mOptIn *protojson.MarshalOptions, uOptIn *protojson
MarshalOptions: *mOpt,
UnmarshalOptions: *uOpt,
}),
runtime.WithMetadata(func(c context.Context, req *http.Request) metadata.MD {
// we are unable to get scheme with req.URL.Scheme.
// Let's check with TLS.
scheme := "http"
if req.TLS != nil {
scheme = "https"
}

return metadata.Pairs(
"x-forwarded-method", req.Method,
"x-forwarded-path", req.URL.Path,
"x-forwarded-scheme", scheme,
"x-forwarded-remote-addr", req.RemoteAddr,
"x-forwarded-user-agent", req.UserAgent())
}),
runtime.WithMetadata(rkGwMetadataBuilder),

runtime.WithOutgoingHeaderMatcher(OutgoingHeaderMatcher),
runtime.WithIncomingHeaderMatcher(IncomingHeaderMatcher),
}
}

func rkGwMetadataBuilder(c context.Context, req *http.Request) metadata.MD {
// we are unable to get scheme with req.URL.Scheme.
// Let's check with TLS.
scheme := "http"
if req.TLS != nil {
scheme = "https"
}

md := metadata.Pairs(
"x-forwarded-method", req.Method,
"x-forwarded-path", req.URL.Path,
"x-forwarded-scheme", scheme,
"x-forwarded-remote-addr", req.RemoteAddr,
"x-forwarded-user-agent", req.UserAgent())
if pattern, ok := runtime.HTTPPathPattern(c); ok {
md["x-forwarded-pattern"] = []string{pattern}
}

return md
}

// HttpErrorHandler Mainly copies from runtime.DefaultHTTPErrorHandler.
// We reformat error response with rkerror.ErrorResp.
func HttpErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
Expand Down
23 changes: 20 additions & 3 deletions boot/gw_server_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ package rkgrpc
import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/stretchr/testify/assert"
testhttp "github.com/stretchr/testify/http"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/encoding/protojson"
"gopkg.in/yaml.v3"
"io"
"net/http"
"testing"
)

type FakeEncoder struct{}
Expand Down Expand Up @@ -280,6 +282,21 @@ func TestNewRkGwServerMuxOptions(t *testing.T) {
assert.Len(t, opts, 5)
}

func TestRkGwMetadataBuilder(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/uc-path", nil)
ctxAnnotator := runtime.WithHTTPPathPattern("/uc/path/{id}")
ctx := ctxAnnotator(context.Background())

md := rkGwMetadataBuilder(ctx, req)
assert.Equal(t, metadata.Pairs(
"x-forwarded-method", req.Method,
"x-forwarded-path", req.URL.Path,
"x-forwarded-scheme", "http",
"x-forwarded-remote-addr", req.RemoteAddr,
"x-forwarded-user-agent", req.UserAgent(),
"x-forwarded-pattern", "/uc/path/{id}"), md)
}

// ************ Test utility ************

func assertNotPanic(t *testing.T) {
Expand Down
11 changes: 6 additions & 5 deletions middleware/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ package rkgrpcmid

import (
"context"
"github.com/rookie-ninja/rk-entry/v2/middleware"
"go.uber.org/zap"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"net"
"path"
"strings"

rkmid "github.com/rookie-ninja/rk-entry/v2/middleware"
"go.uber.org/zap"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
)

var (
Expand Down Expand Up @@ -42,7 +43,7 @@ func GetGwInfo(md metadata.MD) (gwMethod, gwPath, gwScheme, gwUserAgent string)
gwMethod = tokens[0]
}

if tokens := md["x-forwarded-path"]; len(tokens) > 0 {
if tokens := md["x-forwarded-pattern"]; len(tokens) > 0 {
gwPath = tokens[0]
}

Expand Down
7 changes: 4 additions & 3 deletions middleware/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ package rkgrpcmid

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"testing"
)

type FakeAddr struct{}
Expand All @@ -26,14 +27,14 @@ func (f FakeAddr) String() string {
func TestGetGwInfo(t *testing.T) {
md := metadata.New(map[string]string{
"x-forwarded-method": "ut-method",
"x-forwarded-path": "ut-path",
"x-forwarded-scheme": "ut-scheme",
"x-forwarded-user-agent": "ut-agent",
"x-forwarded-pattern": "ut-path/{id}",
})

method, path, scheme, agent := GetGwInfo(md)
assert.Equal(t, "ut-method", method)
assert.Equal(t, "ut-path", path)
assert.Equal(t, "ut-path/{id}", path)
assert.Equal(t, "ut-scheme", scheme)
assert.Equal(t, "ut-agent", agent)
}
Expand Down

0 comments on commit d4d0cc5

Please sign in to comment.