Skip to content

Latest commit

 

History

History
99 lines (77 loc) · 2.04 KB

10-wasm-cli-with-extism-go.md

File metadata and controls

99 lines (77 loc) · 2.04 KB

Create an Extism Host application (a CLI)

The application will need 4 arguments:

  • the path to the wasm file
  • the name of the exported function
  • a string parameter to pass to the function
  • a json string to pass a config value

The goal is to be able to re-use and call the 3 previous plugins like this:

hostapp \
../07-extism-go-plug-in/go-plugin.wasm \
hello \
"👋 John Doe" \
'{"url":"https://jsonplaceholder.typicode.com/todos/1"}'
hostapp \
../08-extism-rust-plug-in/target/wasm32-wasi/release/hello_extism_rust.wasm \
hello \
"👩 Jane Doe" \
'{"text":"Hello I am Jane Doe 😊"}'
hostapp \
go run main.go \
../09-extism-js-plug-in/hello-js.wasm \
hello \
"Bob Morane" \
'{}'

With Go

To create an instance of the wasm plug-in, the code looks like this:

wasmPlugin, err := extism.NewPlugin(ctx, pluginManifest, pluginConfig, nil)
  • the type of pluginManifest is extism.Manifest
  • the type of pluginConfig is extism.PluginConfig

To call the exported function, the code looks like this:

_, result, err := wasmPlugin.Call(
    functionName,
    []byte(input),
)

Some help

// Plugin config
pluginConfig := extism.PluginConfig{
    ModuleConfig: wazero.NewModuleConfig().WithSysWalltime(),
    EnableWasi:   true,
    LogLevel:     extism.LogLevelInfo,
}

// Plugin manifest
pluginManifest := extism.Manifest{
    Wasm: []extism.Wasm{
        extism.WasmFile{Path: wasmFilePath},
    },
    AllowedHosts: []string{"*"},
    Config:       configMap,
}


// configMap is a map[string]string
var configMap map[string]string
err := json.Unmarshal([]byte(config), &configMap)
if err != nil {
    fmt.Println("Error converting config to map:", err)
    os.Exit(1)
}

Build and run the application

You can build the application with:

go build -ldflags="-s -w"
ls -lh hostapp

And then call the hostapp application

or run directly the code with go run main.go