diff --git a/.gitignore b/.gitignore index c2b1247..71af4cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ grpcox log -*.out \ No newline at end of file +*.out +vendor/ +.idea \ No newline at end of file diff --git a/core/resource.go b/core/resource.go index c2ac7cd..4c80b2c 100644 --- a/core/resource.go +++ b/core/resource.go @@ -184,7 +184,7 @@ func (r *Resource) Describe(symbol string) (string, string, error) { } // Invoke - invoking gRPC function -func (r *Resource) Invoke(ctx context.Context, symbol string, in io.Reader) (string, time.Duration, error) { +func (r *Resource) Invoke(ctx context.Context, metadata []string, symbol string, in io.Reader) (string, time.Duration, error) { err := r.openDescriptor() if err != nil { return "", 0, err @@ -199,8 +199,13 @@ func (r *Resource) Invoke(ctx context.Context, symbol string, in io.Reader) (str } h := grpcurl.NewDefaultEventHandler(&resultBuffer, r.descSource, formatter, false) + var headers = r.headers + if len(metadata) != 0 { + headers = metadata + } + start := time.Now() - err = grpcurl.InvokeRPC(ctx, r.descSource, r.clientConn, symbol, r.headers, h, rf.Next) + err = grpcurl.InvokeRPC(ctx, r.descSource, r.clientConn, symbol, headers, h, rf.Next) end := time.Now().Sub(start) / time.Millisecond if err != nil { return "", end, err diff --git a/handler/handler.go b/handler/handler.go index 373e269..ad2c2d6 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -221,8 +221,26 @@ func (h *Handler) invokeFunction(w http.ResponseWriter, r *http.Request) { return } + // context metadata + metadataHeader := r.Header.Get("Metadata") + metadataArr := strings.Split(metadataHeader, ",") + + // construct array of string with "key: value" form to satisfy grpcurl MetadataFromHeaders + var metadata []string + var metadataStr string + for i, m := range metadataArr { + i += 1 + if isEven := i % 2 == 0; isEven { + metadataStr = metadataStr+m + metadata = append(metadata, metadataStr) + metadataStr = "" + continue + } + metadataStr = fmt.Sprintf("%s:", m) + } + // get param - result, timer, err := res.Invoke(context.Background(), funcName, r.Body) + result, timer, err := res.Invoke(context.Background(), metadata, funcName, r.Body) if err != nil { writeError(w, err) return diff --git a/index/index.html b/index/index.html index cf77705..9896a5c 100644 --- a/index/index.html +++ b/index/index.html @@ -47,6 +47,50 @@
+ +
+
+ + +
+
+ + + +