Skip to content

Commit

Permalink
fix review, move GuestConfig out from common internal code
Browse files Browse the repository at this point in the history
Signed-off-by: zhangchao <[email protected]>
  • Loading branch information
Taction committed Jul 18, 2023
1 parent 6cdf1e0 commit b882341
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 0 additions & 4 deletions internal/wasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`

Expand Down
15 changes: 14 additions & 1 deletion middleware/http/wasm/httpwasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand All @@ -36,19 +41,27 @@ 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),
handler.ModuleConfig(wasm.NewModuleConfig(meta).
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
}
Expand Down

0 comments on commit b882341

Please sign in to comment.