From 08d5293841a809566969193fb7179a141609ab59 Mon Sep 17 00:00:00 2001 From: "C.C" Date: Sat, 7 Oct 2023 17:53:37 +0800 Subject: [PATCH] docs: add wasm-go and sfn.wait() --- docs/.gitignore | 1 + docs/pages/docs/_meta.json | 1 + docs/pages/docs/api/sfn.mdx | 4 ++ docs/pages/docs/wasm-go.mdx | 83 +++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 docs/pages/docs/wasm-go.mdx diff --git a/docs/.gitignore b/docs/.gitignore index a9324b0aa..1279e376f 100644 --- a/docs/.gitignore +++ b/docs/.gitignore @@ -10,3 +10,4 @@ out next-env.d.ts .next .vercel +public diff --git a/docs/pages/docs/_meta.json b/docs/pages/docs/_meta.json index 737fb28cb..78ef7dc79 100644 --- a/docs/pages/docs/_meta.json +++ b/docs/pages/docs/_meta.json @@ -14,6 +14,7 @@ "wasm-rust": "Write StreamFunction in Rust", "wasm-zig": "Write StreamFunction in Zig", "wasm-c": "Write StreamFunction in C", + "wasm-go": "Write StreamFunction in Go", "---Observability": { "type": "separator", "title": "Observability" diff --git a/docs/pages/docs/api/sfn.mdx b/docs/pages/docs/api/sfn.mdx index 734c2528b..e477a37ea 100644 --- a/docs/pages/docs/api/sfn.mdx +++ b/docs/pages/docs/api/sfn.mdx @@ -118,6 +118,10 @@ Write data to [Zipper][zipper]. Close the connection to [Zipper][zipper]. +### sfn.Wait() + +Wait until the function fulfilled. + ## type SfnOption ### func WithObserveDataTags(tags ...Tag) SfnOption diff --git a/docs/pages/docs/wasm-go.mdx b/docs/pages/docs/wasm-go.mdx new file mode 100644 index 000000000..6ae960dd9 --- /dev/null +++ b/docs/pages/docs/wasm-go.mdx @@ -0,0 +1,83 @@ +--- +title: Implement StreamFunction in Go +--- + +import { Steps, Callout } from 'nextra-theme-docs' + +# Implement StreamFunction in [Golang](https://go.dev/) + + +This feature is currently in alpha and subject to change. + + + +### Install CLI + +```bash +$ curl -fsSL "https://get.yomo.run" | sh +``` + +[Install TinyGo](https://tinygo.org/getting-started/install/) + +on Mac: + +```bash +$ brew tap tinygo-org/tools +$ brew install tinygo +``` + +### Write a StreamFunction in Go + +```go +package main + +import ( + "fmt" + "strings" + + "github.com/yomorun/yomo/serverless" + "github.com/yomorun/yomo/serverless/guest" +) + +func main() { + guest.DataTags = DataTags + guest.Handler = Handler + guest.Init = Init +} + +// Init will initialize the stream function +func Init() error { + fmt.Println("wasm go sfn init") + return nil +} + +func Handler(ctx serverless.Context) { + // load input data + tag := ctx.Tag() + input := ctx.Data() + fmt.Printf("wasm go sfn received %d bytes with tag[%#x]\n", len(input), tag) + + // process app data + output := strings.ToUpper(string(input)) + + // write result to zipper + ctx.Write(0x34, []byte(output)) +} + +func DataTags() []uint32 { + return []uint32{0x33} +} +``` + +### Compile to [WASI](https://wasi.dev/) + +```bash +$ tinygo build -o sfn.wasm -no-debug -target wasi +``` + +### Run Streaming Serverless Function + +```bash +yomo run /path/to/sfn.wasm +``` +