From b882341c4b5b9c0082a71272a51453c64e52c712 Mon Sep 17 00:00:00 2001 From: zhangchao Date: Tue, 18 Jul 2023 11:43:01 +0800 Subject: [PATCH] fix review, move GuestConfig out from common internal code Signed-off-by: zhangchao --- internal/wasm/wasm.go | 4 ---- middleware/http/wasm/httpwasm.go | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/internal/wasm/wasm.go b/internal/wasm/wasm.go index 05bb083afb..c008e5b056 100644 --- a/internal/wasm/wasm.go +++ b/internal/wasm/wasm.go @@ -42,10 +42,6 @@ type InitMetadata struct { // - Random number generators are seeded with a deterministic source. StrictSandbox bool `mapstructure:"strictSandbox"` - // GuestConfig is the guest configuration, assigned during instantiation by handler.GuestConfig. - // Currently only used by the HTTP wasm middleware. - GuestConfig string `mapstructure:"guestConfig"` - // Guest is WebAssembly binary implementing the guest, loaded from URL. Guest []byte `mapstructure:"-"` diff --git a/middleware/http/wasm/httpwasm.go b/middleware/http/wasm/httpwasm.go index df986037f5..7a529afe5e 100644 --- a/middleware/http/wasm/httpwasm.go +++ b/middleware/http/wasm/httpwasm.go @@ -22,6 +22,11 @@ type middleware struct { logger logger.Logger } +type Metadata struct { + // GuestConfig is the guest configuration, assigned during instantiation by handler.GuestConfig. + GuestConfig string `mapstructure:"guestConfig"` +} + func NewMiddleware(logger logger.Logger) dapr.Middleware { return &middleware{logger: logger} } @@ -36,11 +41,19 @@ func (m *middleware) GetHandler(ctx context.Context, metadata dapr.Metadata) (fu // getHandler is extracted for unit testing. func (m *middleware) getHandler(ctx context.Context, metadata dapr.Metadata) (*requestHandler, error) { + // parse common wasm metadata configuration meta, err := wasm.GetInitMetadata(metadata.Base) if err != nil { return nil, fmt.Errorf("wasm: failed to parse metadata: %w", err) } + // parse wasm middleware specific metadata + var middlewareMeta Metadata + err = mdutils.DecodeMetadata(metadata.Base, &middlewareMeta) + if err != nil { + return nil, fmt.Errorf("wasm: failed to parse wasm middleware metadata: %w", err) + } + var stdout, stderr bytes.Buffer mw, err := wasmnethttp.NewMiddleware(ctx, meta.Guest, handler.Logger(m), @@ -48,7 +61,7 @@ func (m *middleware) getHandler(ctx context.Context, metadata dapr.Metadata) (*r WithName(meta.GuestName). WithStdout(&stdout). // reset per request WithStderr(&stderr)), // reset per request - handler.GuestConfig([]byte(meta.GuestConfig))) + handler.GuestConfig([]byte(middlewareMeta.GuestConfig))) if err != nil { return nil, err }