diff --git a/gateway/internal/descriptorsource_test.go b/gateway/internal/descriptorsource_test.go index b9fe893f..0d6e8e37 100644 --- a/gateway/internal/descriptorsource_test.go +++ b/gateway/internal/descriptorsource_test.go @@ -3,6 +3,7 @@ package internal import ( "encoding/base64" "io/ioutil" + "net/http" "os" "testing" @@ -11,7 +12,10 @@ import ( "github.com/zeromicro/go-zero/core/hash" ) -const b64pb = `CpgBCgtoZWxsby5wcm90bxIFaGVsbG8iHQoHUmVxdWVzdBISCgRwaW5nGAEgASgJUgRwaW5nIh4KCFJlc3BvbnNlEhIKBHBvbmcYASABKAlSBHBvbmcyMAoFSGVsbG8SJwoEUGluZxIOLmhlbGxvLlJlcXVlc3QaDy5oZWxsby5SZXNwb25zZUIJWgcuL2hlbGxvYgZwcm90bzM=` +const ( + b64pb = `CpgBCgtoZWxsby5wcm90bxIFaGVsbG8iHQoHUmVxdWVzdBISCgRwaW5nGAEgASgJUgRwaW5nIh4KCFJlc3BvbnNlEhIKBHBvbmcYASABKAlSBHBvbmcyMAoFSGVsbG8SJwoEUGluZxIOLmhlbGxvLlJlcXVlc3QaDy5oZWxsby5SZXNwb25zZUIJWgcuL2hlbGxvYgZwcm90bzM=` + b64pbWithAnnotations = `Cs4EChVnb29nbGUvYXBpL2h0dHAucHJvdG8SCmdvb2dsZS5hcGkieQoESHR0cBIqCgVydWxlcxgBIAMoCzIULmdvb2dsZS5hcGkuSHR0cFJ1bGVSBXJ1bGVzEkUKH2Z1bGx5X2RlY29kZV9yZXNlcnZlZF9leHBhbnNpb24YAiABKAhSHGZ1bGx5RGVjb2RlUmVzZXJ2ZWRFeHBhbnNpb24i2gIKCEh0dHBSdWxlEhoKCHNlbGVjdG9yGAEgASgJUghzZWxlY3RvchISCgNnZXQYAiABKAlIAFIDZ2V0EhIKA3B1dBgDIAEoCUgAUgNwdXQSFAoEcG9zdBgEIAEoCUgAUgRwb3N0EhgKBmRlbGV0ZRgFIAEoCUgAUgZkZWxldGUSFgoFcGF0Y2gYBiABK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mLk1ldGhvZE9wdGlvbnMYsMq8IiABKAsyFC5nb29nbGUuYXBpLkh0dHBSdWxlUgRodHRwQh5aHHByb3RvL3RoaXJkX3BhcnR5L2dvb2dsZS9hcGliBnByb3RvMwrcAwoHYS5wcm90bxIFaGVsbG8aHGdvb2dsZS9hcGkvYW5ub3RhdGlvbnMucHJvdG8iHQoHUmVxdWVzdBISCgRwaW5nGAEgASgJUgRwaW5nIh4KCFJlc3BvbnNlEhIKBHBvbmcYASABKAlSBHBvbmcy2QIKBUhlbGxvEkQKB1BpbmdHZXQSDi5oZWxsby5SZXF1ZXN0Gg8uaGVsbG8uUmVzcG9uc2UiGILT5JMCEhINL3YxL2dldC97Zm9vfToBKhJACghQaW5nUG9zdBIOLmhlbGxvLlJlcXVlc3QaDy5oZWxsby5SZXNwb25zZSITgtPkkwINIggvdjEvcG9zdDoBKhI+CgdQaW5nUHV0Eg4uaGVsbG8uUmVxdWVzdBoPLmhlbGxvLlJlc3BvbnNlIhKC0+STAgwaBy92MS9wdXQ6ASoSRAoKUGluZ0RlbGV0ZRIOLmhlbGxvLlJlcXVlc3QaDy5oZWxsby5SZXNwb25zZSIVgtPkkwIPKgovdjEvZGVsZXRlOgEqEkIKCVBpbmdQYXRjaBIOLmhlbGxvLlJlcXVlc3QaDy5oZWxsby5SZXNwb25zZSIUgtPkkwIOMgkvdjEvcGF0Y2g6ASpCCVoHLi9oZWxsb2IGcHJvdG8z` +) func TestGetMethods(t *testing.T) { tmpfile, err := ioutil.TempFile(os.TempDir(), hash.Md5Hex([]byte(b64pb))) @@ -31,3 +35,44 @@ func TestGetMethods(t *testing.T) { }, }, methods) } + +func TestGetMethodsWithAnnotations(t *testing.T) { + tmpfile, err := ioutil.TempFile(os.TempDir(), hash.Md5Hex([]byte(b64pb))) + assert.Nil(t, err) + b, err := base64.StdEncoding.DecodeString(b64pbWithAnnotations) + assert.Nil(t, err) + assert.Nil(t, ioutil.WriteFile(tmpfile.Name(), b, os.ModeTemporary)) + defer os.Remove(tmpfile.Name()) + + source, err := grpcurl.DescriptorSourceFromProtoSets(tmpfile.Name()) + assert.Nil(t, err) + methods, err := GetMethods(source) + assert.Nil(t, err) + assert.EqualValues(t, []Method{ + { + HttpMethod: http.MethodGet, + HttpPath: "/v1/get/:foo", + RpcPath: "hello.Hello/PingGet", + }, + { + HttpMethod: http.MethodPost, + HttpPath: "/v1/post", + RpcPath: "hello.Hello/PingPost", + }, + { + HttpMethod: http.MethodPut, + HttpPath: "/v1/put", + RpcPath: "hello.Hello/PingPut", + }, + { + HttpMethod: http.MethodDelete, + HttpPath: "/v1/delete", + RpcPath: "hello.Hello/PingDelete", + }, + { + HttpMethod: http.MethodPatch, + HttpPath: "/v1/patch", + RpcPath: "hello.Hello/PingPatch", + }, + }, methods) +} diff --git a/gateway/internal/headerbuilder.go b/gateway/internal/headerprocessor.go similarity index 78% rename from gateway/internal/headerbuilder.go rename to gateway/internal/headerprocessor.go index f5fa6ec6..11181f78 100644 --- a/gateway/internal/headerbuilder.go +++ b/gateway/internal/headerprocessor.go @@ -11,8 +11,8 @@ const ( metadataPrefix = "gateway-" ) -// BuildHeaders builds the headers for the gateway from HTTP headers. -func BuildHeaders(header http.Header) []string { +// ProcessHeaders builds the headers for the gateway from HTTP headers. +func ProcessHeaders(header http.Header) []string { var headers []string for k, v := range header { diff --git a/gateway/internal/headerbuilder_test.go b/gateway/internal/headerprocessor_test.go similarity index 74% rename from gateway/internal/headerbuilder_test.go rename to gateway/internal/headerprocessor_test.go index 883fd2ca..c22f59f6 100644 --- a/gateway/internal/headerbuilder_test.go +++ b/gateway/internal/headerprocessor_test.go @@ -10,12 +10,12 @@ import ( func TestBuildHeadersNoValue(t *testing.T) { req := httptest.NewRequest("GET", "/", nil) req.Header.Add("a", "b") - assert.Nil(t, BuildHeaders(req.Header)) + assert.Nil(t, ProcessHeaders(req.Header)) } func TestBuildHeadersWithValues(t *testing.T) { req := httptest.NewRequest("GET", "/", nil) req.Header.Add("grpc-metadata-a", "b") req.Header.Add("grpc-metadata-b", "b") - assert.EqualValues(t, []string{"gateway-A:b", "gateway-B:b"}, BuildHeaders(req.Header)) + assert.ElementsMatch(t, []string{"gateway-A:b", "gateway-B:b"}, ProcessHeaders(req.Header)) } diff --git a/gateway/server.go b/gateway/server.go index 15d4c3a6..8529ee69 100644 --- a/gateway/server.go +++ b/gateway/server.go @@ -19,20 +19,31 @@ import ( "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" ) -// Server is a gateway server. -type Server struct { - *rest.Server - upstreams []upstream - timeout time.Duration -} +type ( + // Server is a gateway server. + Server struct { + *rest.Server + upstreams []upstream + timeout time.Duration + processHeader func(http.Header) []string + } + + // Option defines the method to customize Server. + Option func(svr *Server) +) // MustNewServer creates a new gateway server. -func MustNewServer(c GatewayConf) *Server { - return &Server{ +func MustNewServer(c GatewayConf, opts ...Option) *Server { + svr := &Server{ Server: rest.MustNewServer(c.RestConf), upstreams: c.Upstreams, timeout: c.Timeout, } + for _, opt := range opts { + opt(svr) + } + + return svr } // Start starts the gateway server. @@ -120,7 +131,7 @@ func (s *Server) buildHandler(source grpcurl.DescriptorSource, resolver jsonpb.A defer can() w.Header().Set(httpx.ContentType, httpx.JsonContentType) - if err := grpcurl.InvokeRPC(ctx, source, cli.Conn(), rpcPath, internal.BuildHeaders(r.Header), + if err := grpcurl.InvokeRPC(ctx, source, cli.Conn(), rpcPath, s.prepareMetadata(r.Header), handler, parser.Next); err != nil { httpx.Error(w, err) } @@ -144,3 +155,20 @@ func (s *Server) createDescriptorSource(cli zrpc.Client, up upstream) (grpcurl.D return source, nil } + +func (s *Server) prepareMetadata(header http.Header) []string { + vals := internal.ProcessHeaders(header) + if s.processHeader != nil { + vals = append(vals, s.processHeader(header)...) + } + + return vals +} + +// WithHeaderProcessor sets a processor to process request headers. +// The returned headers are used as metadata to invoke the RPC. +func WithHeaderProcessor(processHeader func(http.Header) []string) func(*Server) { + return func(s *Server) { + s.processHeader = processHeader + } +}