Skip to content

Commit

Permalink
🌺 gardening modules samples
Browse files Browse the repository at this point in the history
  • Loading branch information
k33g committed Aug 15, 2022
1 parent a9fdd31 commit 6b0da1b
Show file tree
Hide file tree
Showing 28 changed files with 313 additions and 78 deletions.
1 change: 1 addition & 0 deletions wasm_modules/registry-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
capsule
cc
capsule-registry
registry
60 changes: 60 additions & 0 deletions wasm_modules/registry-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Capsule Wasm Registry

> 🚧 wip
## Start the registry

```bash
DOWNLOADED_FILES_PATH="${PWD}/registry/functions"
echo "${DOWNLOADED_FILES_PATH}"

./capsule-registry \
-files="${DOWNLOADED_FILES_PATH}" \
-httpPort=4999
```

## Publish wasm modules to the registry

```bash
curl -X POST http://localhost:4999/upload/k33g/hello/0.0.0 \
-F "file=@./capsule-hello/hello.wasm" \
-F "info=hello function from @k33g" \
-H "Content-Type: multipart/form-data"

curl -X POST http://localhost:4999/upload/k33g/hey/0.0.0 \
-F "file=@./capsule-hey/hey.wasm" \
-F "info=hello hey from @k33g" \
-H "Content-Type: multipart/form-data"
```

## Load and serve the modules

```bash
MESSAGE="💊 Capsule Rocks 🚀" ./capsule \
-wasm=./tmp/hello.wasm \
-url="http://localhost:4999/k33g/hello/0.0.0/hello.wasm" \
-mode=http \
-httpPort=9091

MESSAGE="💊 Capsule Rocks 🚀" ./capsule \
-wasm=./tmp/hey.wasm \
-url="http://localhost:4999/k33g/hey/0.0.0/hey.wasm" \
-mode=http \
-httpPort=9092
```

## Call the functions

```bash
curl -v -X POST \
http://localhost:9091 \
-H 'content-type: application/json; charset=utf-8' \
-d '{"message": "Golang 💚💜 wasm", "author": "Bob Morane"}'
```

```bash
curl -v -X POST \
http://localhost:9092 \
-H 'content-type: application/json; charset=utf-8' \
-d '{"message": "Golang 💚💜 wasm", "author": "Bill Murray"}'
```
11 changes: 1 addition & 10 deletions wasm_modules/registry-demo/build-all.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
#!/bin/bash

cd ../with-proxy/capsule-hello
cd ./capsule-hello
./build.sh

cd ../capsule-hey
./build.sh

cd ../capsule-hola
./build.sh

cd ../capsule-hola-orange
./build.sh

cd ../capsule-hola-yellow
./build.sh

4 changes: 4 additions & 0 deletions wasm_modules/registry-demo/capsule-hello/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
tinygo build -o hello.wasm -scheduler=none -target wasi ./hello.go

ls -lh *.wasm
15 changes: 15 additions & 0 deletions wasm_modules/registry-demo/capsule-hello/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/bots-garden/capsule/wasm_modules/registry-demo/capsule-hello

go 1.18

require (
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2
github.com/tidwall/gjson v1.14.2
github.com/tidwall/sjson v1.2.5
)

require (
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)
12 changes: 12 additions & 0 deletions wasm_modules/registry-demo/capsule-hello/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3 h1:jd6GhVtU6hJh2YhTOal46CxbSfGALQOw+QdwC4N/7eg=
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3/go.mod h1:PBhXY24j6vjuYghjEt+9mXzDb2ZwD9m9PAh9e6FTXck=
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2 h1:SfgCTdHbmOkcrxlgvdjJiQaue9LsxAW6doCGOI2wxBE=
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2/go.mod h1:tu9DYBPXGSfiDaCN1opAFZZ3FFt3TVFH5jkYnPypY64=
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
60 changes: 60 additions & 0 deletions wasm_modules/registry-demo/capsule-hello/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

// TinyGo wasm module
import (
hf "github.com/bots-garden/capsule/capsulemodule/hostfunctions"
/* string to json */
"github.com/tidwall/gjson"
/* create json string */
"github.com/tidwall/sjson"
)

// main is required.
func main() {

hf.SetHandleHttp(Handle)
}

func Handle(bodyReq string, headersReq map[string]string) (bodyResp string, headersResp map[string]string, errResp error) {
/*
bodyReq = {"author":"Philippe","message":"Golang 💚 wasm"}
*/
hf.Log("📝 body: " + bodyReq)

author := gjson.Get(bodyReq, "author")
message := gjson.Get(bodyReq, "message")
hf.Log("👋 " + message.String() + " by " + author.String() + " 😄")

hf.Log("Content-Type: " + headersReq["Content-Type"])
hf.Log("Content-Length: " + headersReq["Content-Length"])
hf.Log("User-Agent: " + headersReq["User-Agent"])

envMessage, err := hf.GetEnv("MESSAGE")
if err != nil {
hf.Log("😡 " + err.Error())
} else {
hf.Log("Environment variable: " + envMessage)
}

headersResp = map[string]string{
"Content-Type": "application/json; charset=utf-8",
"Message": "👋 hello world 🌍",
}

jsondoc := `{"message": "", "author": ""}`
jsondoc, _ = sjson.Set(jsondoc, "message", "👋 Hello! What's up?")
jsondoc, _ = sjson.Set(jsondoc, "author", "Bob")

return jsondoc, headersResp, nil
//return jsondoc, headersResp , errors.New("😡 oups I did it again")
}

// TODO: helpers: SetHeader() ...
// TODO: be able to return a status code

/*
curl -v -X POST \
http://localhost:9091 \
-H 'content-type: application/json' \
-d '{"message": "Golang 💚 wasm"}'
*/
5 changes: 5 additions & 0 deletions wasm_modules/registry-demo/capsule-hello/query.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
curl -v -X POST \
http://localhost:9091 \
-H 'content-type: application/json; charset=utf-8' \
-d '{"message": "Golang 💚💜 wasm", "author": "Philippe"}'
8 changes: 8 additions & 0 deletions wasm_modules/registry-demo/capsule-hello/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
cd ../../../capsule-launcher

export MESSAGE="💊 Capsule Rocks 🚀"
go run main.go \
-wasm=../wasm_modules/registry-demo/capsule-hello/hello.wasm \
-mode=http \
-httpPort=9091
4 changes: 4 additions & 0 deletions wasm_modules/registry-demo/capsule-hey/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
tinygo build -o hey.wasm -scheduler=none -target wasi ./hey.go

ls -lh *.wasm
15 changes: 15 additions & 0 deletions wasm_modules/registry-demo/capsule-hey/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/bots-garden/capsule/wasm_modules/registry-demo/capsule-hey

go 1.18

require (
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2
github.com/tidwall/gjson v1.14.2
github.com/tidwall/sjson v1.2.5
)

require (
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)
12 changes: 12 additions & 0 deletions wasm_modules/registry-demo/capsule-hey/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3 h1:jd6GhVtU6hJh2YhTOal46CxbSfGALQOw+QdwC4N/7eg=
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3/go.mod h1:PBhXY24j6vjuYghjEt+9mXzDb2ZwD9m9PAh9e6FTXck=
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2 h1:SfgCTdHbmOkcrxlgvdjJiQaue9LsxAW6doCGOI2wxBE=
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2/go.mod h1:tu9DYBPXGSfiDaCN1opAFZZ3FFt3TVFH5jkYnPypY64=
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
60 changes: 60 additions & 0 deletions wasm_modules/registry-demo/capsule-hey/hey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

// TinyGo wasm module
import (
hf "github.com/bots-garden/capsule/capsulemodule/hostfunctions"
/* string to json */
"github.com/tidwall/gjson"
/* create json string */
"github.com/tidwall/sjson"
)

// main is required.
func main() {

hf.SetHandleHttp(Handle)
}

func Handle(bodyReq string, headersReq map[string]string) (bodyResp string, headersResp map[string]string, errResp error) {
/*
bodyReq = {"author":"Philippe","message":"Golang 💚 wasm"}
*/
hf.Log("📝 body: " + bodyReq)

author := gjson.Get(bodyReq, "author")
message := gjson.Get(bodyReq, "message")
hf.Log("👋 " + message.String() + " by " + author.String() + " 😄")

hf.Log("Content-Type: " + headersReq["Content-Type"])
hf.Log("Content-Length: " + headersReq["Content-Length"])
hf.Log("User-Agent: " + headersReq["User-Agent"])

envMessage, err := hf.GetEnv("MESSAGE")
if err != nil {
hf.Log("😡 " + err.Error())
} else {
hf.Log("Environment variable: " + envMessage)
}

headersResp = map[string]string{
"Content-Type": "application/json; charset=utf-8",
"Message": "👋 hey people 🌍",
}

jsondoc := `{"message": "", "author": ""}`
jsondoc, _ = sjson.Set(jsondoc, "message", "👋 Hey! What's up?")
jsondoc, _ = sjson.Set(jsondoc, "author", "Bob")

return jsondoc, headersResp, nil
//return jsondoc, headersResp , errors.New("😡 oups I did it again")
}

// TODO: helpers: SetHeader() ...
// TODO: be able to return a status code

/*
curl -v -X POST \
http://localhost:9092 \
-H 'content-type: application/json' \
-d '{"message": "Golang 💚 wasm"}'
*/
5 changes: 5 additions & 0 deletions wasm_modules/registry-demo/capsule-hey/query.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
curl -v -X POST \
http://localhost:9092 \
-H 'content-type: application/json; charset=utf-8' \
-d '{"message": "Golang 💚💜 wasm", "author": "Philippe"}'
8 changes: 8 additions & 0 deletions wasm_modules/registry-demo/capsule-hey/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
cd ../../../capsule-launcher

export MESSAGE="💊 Capsule is Amazing 😍"
go run main.go \
-wasm=../wasm_modules/registry-demo/capsule-hey/hey.wasm \
-mode=http \
-httpPort=9092
11 changes: 11 additions & 0 deletions wasm_modules/registry-demo/load-run-http-hello.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

cd ../../capsule-launcher

export MESSAGE="💊 Capsule Rocks 🚀"
go run main.go \
-wasm=./tmp/hello.wasm \
-url="http://localhost:4999/k33g/hello/0.0.0/hello.wasm" \
-mode=http \
-httpPort=9091

12 changes: 12 additions & 0 deletions wasm_modules/registry-demo/load-run-http-hey.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

cd ../../capsule-launcher

export MESSAGE="💊 Capsule Rocks 🚀"
go run main.go \
-wasm=./tmp/hey.wasm \
-url="http://localhost:4999/k33g/hey/0.0.0/hey.wasm" \
-mode=http \
-httpPort=9092


11 changes: 0 additions & 11 deletions wasm_modules/registry-demo/load-run-http-hola-orange.sh

This file was deleted.

11 changes: 0 additions & 11 deletions wasm_modules/registry-demo/load-run-http-hola-yellow.sh

This file was deleted.

2 changes: 0 additions & 2 deletions wasm_modules/registry-demo/notes.txt

This file was deleted.

11 changes: 11 additions & 0 deletions wasm_modules/registry-demo/publish-wasm-modules-to-registry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

curl -X POST http://localhost:4999/upload/k33g/hello/0.0.0 \
-F "file=@./capsule-hello/hello.wasm" \
-F "info=hello function from @k33g" \
-H "Content-Type: multipart/form-data"

curl -X POST http://localhost:4999/upload/k33g/hey/0.0.0 \
-F "file=@./capsule-hey/hey.wasm" \
-F "info=hello hey from @k33g" \
-H "Content-Type: multipart/form-data"
27 changes: 0 additions & 27 deletions wasm_modules/registry-demo/publish.sh

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello function from @k33g
Loading

0 comments on commit 6b0da1b

Please sign in to comment.