mirror of https://github.com/dapr/docs.git
Merge pull request #3632 from Taction/add-wasm-middleware-config
Add guestConfig metadata doc for wasm middleware
This commit is contained in:
commit
9ded13d0a8
|
@ -39,6 +39,8 @@ spec:
|
||||||
metadata:
|
metadata:
|
||||||
- name: url
|
- name: url
|
||||||
value: "file://router.wasm"
|
value: "file://router.wasm"
|
||||||
|
- guestConfig
|
||||||
|
value: {"environment":"production"}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Spec metadata fields
|
## Spec metadata fields
|
||||||
|
@ -49,6 +51,7 @@ How to compile this is described later.
|
||||||
| Field | Details | Required | Example |
|
| Field | Details | Required | Example |
|
||||||
|-------|----------------------------------------------------------------|----------|----------------|
|
|-------|----------------------------------------------------------------|----------|----------------|
|
||||||
| url | The URL of the resource including the Wasm binary to instantiate. The supported schemes include `file://`, `http://`, and `https://`. The path of a `file://` URL is relative to the Dapr process unless it begins with `/`. | true | `file://hello.wasm`, `https://example.com/hello.wasm` |
|
| url | The URL of the resource including the Wasm binary to instantiate. The supported schemes include `file://`, `http://`, and `https://`. The path of a `file://` URL is relative to the Dapr process unless it begins with `/`. | true | `file://hello.wasm`, `https://example.com/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"}` |
|
||||||
|
|
||||||
## Dapr configuration
|
## Dapr configuration
|
||||||
|
|
||||||
|
@ -120,6 +123,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`
|
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.
|
||||||
|
|
||||||
|
```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
|
## Related links
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue