From ff722225b546c29ff7d83f2ba527f816073c7d26 Mon Sep 17 00:00:00 2001 From: zhangchao Date: Tue, 25 Jul 2023 15:48:07 +0800 Subject: [PATCH 1/5] add guestConfig metadata doc for wasm middleware Signed-off-by: zhangchao --- .../supported-middleware/middleware-wasm.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md b/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md index 51d75b0b88d..c93bf395374 100644 --- a/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md +++ b/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md @@ -35,6 +35,8 @@ spec: metadata: - name: url value: "file://router.wasm" + - guestConfig + value: {"environment":"production"} ``` ## Spec metadata fields @@ -45,6 +47,7 @@ How to compile this is described later. | Field | Details | Required | Example | |-------|----------------------------------------------------------------|----------|----------------| | url | The URL of the resource including the Wasm binary to instantiate. The supported schemes include `file://`. The path of a `file://` URL is relative to the Dapr process unless it begins with `/`. | true | `file://hello.wasm` | +| guestConfig | The configuration for the wasm function, its format depends on how the wasm function parses it. | false | `enviroment=production`,`{"environment":"production"}` | ## Dapr configuration From c4cd7bdb2c1b2852f297dc571ae87102c19eb9f1 Mon Sep 17 00:00:00 2001 From: zhangchao Date: Tue, 25 Jul 2023 22:59:10 +0800 Subject: [PATCH 2/5] add example Signed-off-by: zhangchao --- .../supported-middleware/middleware-wasm.md | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md b/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md index c93bf395374..f81b2a6f5f9 100644 --- a/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md +++ b/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md @@ -47,7 +47,7 @@ How to compile this is described later. | Field | Details | Required | Example | |-------|----------------------------------------------------------------|----------|----------------| | url | The URL of the resource including the Wasm binary to instantiate. The supported schemes include `file://`. The path of a `file://` URL is relative to the Dapr process unless it begins with `/`. | true | `file://hello.wasm` | -| guestConfig | The configuration for the wasm function, its format depends on how the wasm function parses it. | false | `enviroment=production`,`{"environment":"production"}` | +| guestConfig | An optional configuration passed to WASM guests. Users can pass an arbitrary string to be parsed by the guest code. | false | `enviroment=production`,`{"environment":"production"}` | ## Dapr configuration @@ -119,6 +119,49 @@ If using TinyGo, compile as shown below and set the spec metadata field named tinygo build -o router.wasm -scheduler=none --no-debug -target=wasi router.go` ``` +### wasm config example + +Here is an example of how to use `guestConfig` to pass configurations to wasm. In wasm code, you can use the function `handler.Host.GetConfig` defined in guest sdk to get the configuration. In the following example, the wasm middleware parses the executed `environment` from json config defined in the component. + +```yaml +apiVersion: dapr.io/v1alpha1 +kind: Component +metadata: + name: wasm +spec: + type: middleware.http.wasm + version: v1 + metadata: + - name: url + value: "file://router.wasm" + - guestConfig + value: {"environment":"production"} +``` +Here's an example in TinyGo: + +```go +package main + +import ( + "encoding/json" + "github.com/http-wasm/http-wasm-guest-tinygo/handler" + "github.com/http-wasm/http-wasm-guest-tinygo/handler/api" +) + +type Config struct { + Environment string `json:"environment"` +} + +func main() { + // get config bytes, which is the value of guestConfig defined in the component. + configBytes := handler.Host.GetConfig() + + config := Config{} + json.Unmarshal(configBytes, &config) + handler.Host.Log(api.LogLevelInfo, "Config environment: "+config.Environment) +} +``` + ## Related links From 7710a4a51c4d394561f083fef8d17182e53cff96 Mon Sep 17 00:00:00 2001 From: Taction Date: Wed, 2 Aug 2023 13:55:00 +0800 Subject: [PATCH 3/5] Update daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md Co-authored-by: Hannah Hunter <94493363+hhunter-ms@users.noreply.github.com> Signed-off-by: Taction --- .../supported-middleware/middleware-wasm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md b/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md index f81b2a6f5f9..876bfc51eb2 100644 --- a/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md +++ b/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md @@ -47,7 +47,7 @@ How to compile this is described later. | Field | Details | Required | Example | |-------|----------------------------------------------------------------|----------|----------------| | url | The URL of the resource including the Wasm binary to instantiate. The supported schemes include `file://`. The path of a `file://` URL is relative to the Dapr process unless it begins with `/`. | true | `file://hello.wasm` | -| guestConfig | An optional configuration passed to WASM guests. Users can pass an arbitrary string to be parsed by the guest code. | false | `enviroment=production`,`{"environment":"production"}` | +| guestConfig | An optional configuration passed to Wasm guests. Users can pass an arbitrary string to be parsed by the guest code. | false | `enviroment=production`,`{"environment":"production"}` | ## Dapr configuration From 67f25e9b4f4a00ab7b0b5629f413069e42582047 Mon Sep 17 00:00:00 2001 From: Taction Date: Wed, 2 Aug 2023 13:55:11 +0800 Subject: [PATCH 4/5] Update daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md Co-authored-by: Hannah Hunter <94493363+hhunter-ms@users.noreply.github.com> Signed-off-by: Taction --- .../supported-middleware/middleware-wasm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md b/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md index 876bfc51eb2..93e6ff629d7 100644 --- a/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md +++ b/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md @@ -119,7 +119,7 @@ If using TinyGo, compile as shown below and set the spec metadata field named tinygo build -o router.wasm -scheduler=none --no-debug -target=wasi router.go` ``` -### wasm config example +### Wasm `guestConfig` example Here is an example of how to use `guestConfig` to pass configurations to wasm. In wasm code, you can use the function `handler.Host.GetConfig` defined in guest sdk to get the configuration. In the following example, the wasm middleware parses the executed `environment` from json config defined in the component. From a2b1b1998c4ed304aee9fcddb16b02b4d2ae6d72 Mon Sep 17 00:00:00 2001 From: Taction Date: Wed, 2 Aug 2023 13:55:52 +0800 Subject: [PATCH 5/5] Update daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md Co-authored-by: Hannah Hunter <94493363+hhunter-ms@users.noreply.github.com> Signed-off-by: Taction --- .../supported-middleware/middleware-wasm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md b/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md index 93e6ff629d7..3d91665105f 100644 --- a/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md +++ b/daprdocs/content/en/reference/components-reference/supported-middleware/middleware-wasm.md @@ -121,7 +121,7 @@ tinygo build -o router.wasm -scheduler=none --no-debug -target=wasi router.go` ### Wasm `guestConfig` example -Here is an example of how to use `guestConfig` to pass configurations to wasm. In wasm code, you can use the function `handler.Host.GetConfig` defined in guest sdk to get the configuration. In the following example, the wasm middleware parses the executed `environment` from json config defined in the component. +Here is an example of how to use `guestConfig` to pass configurations to Wasm. In Wasm code, you can use the function `handler.Host.GetConfig` defined in guest SDK to get the configuration. In the following example, the Wasm middleware parses the executed `environment` from JSON config defined in the component. ```yaml apiVersion: dapr.io/v1alpha1