Skip to content
This repository has been archived by the owner on Feb 26, 2025. It is now read-only.

Commit

Permalink
go example working
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuwalow committed Jan 30, 2025
1 parent 2b6dc33 commit 27dd624
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/go/go-default-wasi-http/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module pack/name

go 1.20

require github.com/golemcloud/golem-go v1.1.0
2 changes: 2 additions & 0 deletions examples/go/go-default-wasi-http/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/golemcloud/golem-go v1.1.0 h1:By+OBuRu4c3WTuf2OKh71yY7rgOyLXzUgr7Y+98BjGk=
github.com/golemcloud/golem-go v1.1.0/go.mod h1:VLL22qVo5R2+jGLO43tLPpPjf2WrA6B7GQoqXKnSODo=
49 changes: 49 additions & 0 deletions examples/go/go-default-wasi-http/golem.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Schema for IDEA:
# $schema: https://schema.golem.cloud/app/golem/1.1.1/golem.schema.json
# Schema for vscode-yaml
# yaml-language-server: $schema=https://schema.golem.cloud/app/golem/1.1.1/golem.schema.json

tempDir: build/golem-temp
components:
pack:name:
sourceWit: wit
generatedWit: wit-generated
componentWasm: build/adapted-components/component_name.wasm
linkedWasm: build/linked-components/component_name_linked.wasm
build:
- command: wit-bindgen tiny-go --rename-package binding --out-dir binding ./wit-generated
rmdirs:
- binding
mkdirs:
- binding
sources:
- wit-generated
targets:
- binding
- command: tinygo build -target=wasi -tags=purego -o build/components/component_name.wasm main.go
mkdirs:
- build/components
sources:
- component_name
- main.go
targets:
- build/components/component_name.wasm
- command: wasm-tools component embed wit-generated build/components/component_name.wasm --output build/embedded-components/component_name.wasm
mkdirs:
- build/embedded-components
sources:
- wit-generated
- build/components/component_name.wasm
targets:
- build/embedded-components/component_name.wasm
- command: wasm-tools component new build/embedded-components/component_name.wasm -o build/adapted-components/component_name.wasm --adapt adapters/tier1/wasi_snapshot_preview1.wasm
mkdirs:
- build/adapted-components
sources:
- adapters/tier1/wasi_snapshot_preview1.wasm
- build/embedded-components/component_name.wasm
targets:
- build/adapted-components/component_name.wasm
clean:
- build
- binding
33 changes: 33 additions & 0 deletions examples/go/go-default-wasi-http/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"pack/name/binding"
)

// Helper type aliases to make code more readable
type HttpRequest = binding.ExportsWasiHttp0_2_0_IncomingHandlerIncomingRequest
type HttpResponseOut = binding.ExportsWasiHttp0_2_0_IncomingHandlerResponseOutparam
type HttpOutgoingResponse = binding.WasiHttp0_2_0_TypesOutgoingResponse
type HttpError = binding.WasiHttp0_2_0_TypesErrorCode

func init() {
binding.SetExportsWasiHttp0_2_0_IncomingHandler(&ComponentNameImpl{})
}

type ComponentNameImpl struct {}

// Implementation of the exported interface

func (e *ComponentNameImpl) Handle(request HttpRequest, responseOut HttpResponseOut) {
// Construct HttpResponse to send back
headers := binding.NewFields()
httpResponse := binding.NewOutgoingResponse(headers)
httpResponse.SetStatusCode(200)
httpResponse.Body().Unwrap().Write().Unwrap().BlockingWriteAndFlush([]uint8("Hello from Go!\n")).Unwrap()

// Send HTTP response
okResponse := binding.Ok[HttpOutgoingResponse, HttpError](httpResponse)
binding.StaticResponseOutparamSet(responseOut, okResponse)
}

func main() {}
13 changes: 13 additions & 0 deletions examples/go/go-default-wasi-http/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"description": "A Golem worker implementing wasi:http/incoming-handler. For use with the http-handler api gateway binding type",
"requiresAdapter": true,
"adapterTarget": "adapters",
"requiresGolemHostWIT": true,
"requiresWASI": true,
"instructions": "INSTRUCTIONS-app",
"exclude": [
"wit-generated",
"build",
"main.wasm"
]
}
12 changes: 12 additions & 0 deletions examples/go/go-default-wasi-http/wit/component-name.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pack:name;

// See https://component-model.bytecodealliance.org/design/wit.html for more details about the WIT syntax
// See https://github.com/WebAssembly/wasi-http for an introduction to wasi-http

world component-name {
// Always required due to a limitation of the bindings generation in componentize-py.
// This import will fail if removed https://github.com/bytecodealliance/componentize-py/blob/c50822c825b4333ff41a0ea3cd9e0c9bc3df49da/bundled/poll_loop.py#L15
import wasi:http/outgoing-handler@0.2.0;

export wasi:http/incoming-handler@0.2.0;
}

0 comments on commit 27dd624

Please sign in to comment.