Skip to content

Commit 6b0da1b

Browse files
committed
🌺 gardening modules samples
1 parent a9fdd31 commit 6b0da1b

28 files changed

+313
-78
lines changed

wasm_modules/registry-demo/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
capsule
22
cc
33
capsule-registry
4+
registry

wasm_modules/registry-demo/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Capsule Wasm Registry
2+
3+
> 🚧 wip
4+
5+
## Start the registry
6+
7+
```bash
8+
DOWNLOADED_FILES_PATH="${PWD}/registry/functions"
9+
echo "${DOWNLOADED_FILES_PATH}"
10+
11+
./capsule-registry \
12+
-files="${DOWNLOADED_FILES_PATH}" \
13+
-httpPort=4999
14+
```
15+
16+
## Publish wasm modules to the registry
17+
18+
```bash
19+
curl -X POST http://localhost:4999/upload/k33g/hello/0.0.0 \
20+
-F "file=@./capsule-hello/hello.wasm" \
21+
-F "info=hello function from @k33g" \
22+
-H "Content-Type: multipart/form-data"
23+
24+
curl -X POST http://localhost:4999/upload/k33g/hey/0.0.0 \
25+
-F "file=@./capsule-hey/hey.wasm" \
26+
-F "info=hello hey from @k33g" \
27+
-H "Content-Type: multipart/form-data"
28+
```
29+
30+
## Load and serve the modules
31+
32+
```bash
33+
MESSAGE="💊 Capsule Rocks 🚀" ./capsule \
34+
-wasm=./tmp/hello.wasm \
35+
-url="http://localhost:4999/k33g/hello/0.0.0/hello.wasm" \
36+
-mode=http \
37+
-httpPort=9091
38+
39+
MESSAGE="💊 Capsule Rocks 🚀" ./capsule \
40+
-wasm=./tmp/hey.wasm \
41+
-url="http://localhost:4999/k33g/hey/0.0.0/hey.wasm" \
42+
-mode=http \
43+
-httpPort=9092
44+
```
45+
46+
## Call the functions
47+
48+
```bash
49+
curl -v -X POST \
50+
http://localhost:9091 \
51+
-H 'content-type: application/json; charset=utf-8' \
52+
-d '{"message": "Golang 💚💜 wasm", "author": "Bob Morane"}'
53+
```
54+
55+
```bash
56+
curl -v -X POST \
57+
http://localhost:9092 \
58+
-H 'content-type: application/json; charset=utf-8' \
59+
-d '{"message": "Golang 💚💜 wasm", "author": "Bill Murray"}'
60+
```
+1-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
#!/bin/bash
22

3-
cd ../with-proxy/capsule-hello
3+
cd ./capsule-hello
44
./build.sh
55

66
cd ../capsule-hey
77
./build.sh
88

9-
cd ../capsule-hola
10-
./build.sh
11-
12-
cd ../capsule-hola-orange
13-
./build.sh
14-
15-
cd ../capsule-hola-yellow
16-
./build.sh
17-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
tinygo build -o hello.wasm -scheduler=none -target wasi ./hello.go
3+
4+
ls -lh *.wasm
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/bots-garden/capsule/wasm_modules/registry-demo/capsule-hello
2+
3+
go 1.18
4+
5+
require (
6+
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2
7+
github.com/tidwall/gjson v1.14.2
8+
github.com/tidwall/sjson v1.2.5
9+
)
10+
11+
require (
12+
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3 // indirect
13+
github.com/tidwall/match v1.1.1 // indirect
14+
github.com/tidwall/pretty v1.2.0 // indirect
15+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3 h1:jd6GhVtU6hJh2YhTOal46CxbSfGALQOw+QdwC4N/7eg=
2+
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3/go.mod h1:PBhXY24j6vjuYghjEt+9mXzDb2ZwD9m9PAh9e6FTXck=
3+
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2 h1:SfgCTdHbmOkcrxlgvdjJiQaue9LsxAW6doCGOI2wxBE=
4+
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2/go.mod h1:tu9DYBPXGSfiDaCN1opAFZZ3FFt3TVFH5jkYnPypY64=
5+
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
6+
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
7+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
8+
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
9+
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
10+
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
11+
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
12+
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
// TinyGo wasm module
4+
import (
5+
hf "github.com/bots-garden/capsule/capsulemodule/hostfunctions"
6+
/* string to json */
7+
"github.com/tidwall/gjson"
8+
/* create json string */
9+
"github.com/tidwall/sjson"
10+
)
11+
12+
// main is required.
13+
func main() {
14+
15+
hf.SetHandleHttp(Handle)
16+
}
17+
18+
func Handle(bodyReq string, headersReq map[string]string) (bodyResp string, headersResp map[string]string, errResp error) {
19+
/*
20+
bodyReq = {"author":"Philippe","message":"Golang 💚 wasm"}
21+
*/
22+
hf.Log("📝 body: " + bodyReq)
23+
24+
author := gjson.Get(bodyReq, "author")
25+
message := gjson.Get(bodyReq, "message")
26+
hf.Log("👋 " + message.String() + " by " + author.String() + " 😄")
27+
28+
hf.Log("Content-Type: " + headersReq["Content-Type"])
29+
hf.Log("Content-Length: " + headersReq["Content-Length"])
30+
hf.Log("User-Agent: " + headersReq["User-Agent"])
31+
32+
envMessage, err := hf.GetEnv("MESSAGE")
33+
if err != nil {
34+
hf.Log("😡 " + err.Error())
35+
} else {
36+
hf.Log("Environment variable: " + envMessage)
37+
}
38+
39+
headersResp = map[string]string{
40+
"Content-Type": "application/json; charset=utf-8",
41+
"Message": "👋 hello world 🌍",
42+
}
43+
44+
jsondoc := `{"message": "", "author": ""}`
45+
jsondoc, _ = sjson.Set(jsondoc, "message", "👋 Hello! What's up?")
46+
jsondoc, _ = sjson.Set(jsondoc, "author", "Bob")
47+
48+
return jsondoc, headersResp, nil
49+
//return jsondoc, headersResp , errors.New("😡 oups I did it again")
50+
}
51+
52+
// TODO: helpers: SetHeader() ...
53+
// TODO: be able to return a status code
54+
55+
/*
56+
curl -v -X POST \
57+
http://localhost:9091 \
58+
-H 'content-type: application/json' \
59+
-d '{"message": "Golang 💚 wasm"}'
60+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
curl -v -X POST \
3+
http://localhost:9091 \
4+
-H 'content-type: application/json; charset=utf-8' \
5+
-d '{"message": "Golang 💚💜 wasm", "author": "Philippe"}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
cd ../../../capsule-launcher
3+
4+
export MESSAGE="💊 Capsule Rocks 🚀"
5+
go run main.go \
6+
-wasm=../wasm_modules/registry-demo/capsule-hello/hello.wasm \
7+
-mode=http \
8+
-httpPort=9091
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
tinygo build -o hey.wasm -scheduler=none -target wasi ./hey.go
3+
4+
ls -lh *.wasm
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/bots-garden/capsule/wasm_modules/registry-demo/capsule-hey
2+
3+
go 1.18
4+
5+
require (
6+
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2
7+
github.com/tidwall/gjson v1.14.2
8+
github.com/tidwall/sjson v1.2.5
9+
)
10+
11+
require (
12+
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3 // indirect
13+
github.com/tidwall/match v1.1.1 // indirect
14+
github.com/tidwall/pretty v1.2.0 // indirect
15+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3 h1:jd6GhVtU6hJh2YhTOal46CxbSfGALQOw+QdwC4N/7eg=
2+
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3/go.mod h1:PBhXY24j6vjuYghjEt+9mXzDb2ZwD9m9PAh9e6FTXck=
3+
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2 h1:SfgCTdHbmOkcrxlgvdjJiQaue9LsxAW6doCGOI2wxBE=
4+
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815092415-964aa3e0fdc2/go.mod h1:tu9DYBPXGSfiDaCN1opAFZZ3FFt3TVFH5jkYnPypY64=
5+
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
6+
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
7+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
8+
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
9+
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
10+
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
11+
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
12+
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
// TinyGo wasm module
4+
import (
5+
hf "github.com/bots-garden/capsule/capsulemodule/hostfunctions"
6+
/* string to json */
7+
"github.com/tidwall/gjson"
8+
/* create json string */
9+
"github.com/tidwall/sjson"
10+
)
11+
12+
// main is required.
13+
func main() {
14+
15+
hf.SetHandleHttp(Handle)
16+
}
17+
18+
func Handle(bodyReq string, headersReq map[string]string) (bodyResp string, headersResp map[string]string, errResp error) {
19+
/*
20+
bodyReq = {"author":"Philippe","message":"Golang 💚 wasm"}
21+
*/
22+
hf.Log("📝 body: " + bodyReq)
23+
24+
author := gjson.Get(bodyReq, "author")
25+
message := gjson.Get(bodyReq, "message")
26+
hf.Log("👋 " + message.String() + " by " + author.String() + " 😄")
27+
28+
hf.Log("Content-Type: " + headersReq["Content-Type"])
29+
hf.Log("Content-Length: " + headersReq["Content-Length"])
30+
hf.Log("User-Agent: " + headersReq["User-Agent"])
31+
32+
envMessage, err := hf.GetEnv("MESSAGE")
33+
if err != nil {
34+
hf.Log("😡 " + err.Error())
35+
} else {
36+
hf.Log("Environment variable: " + envMessage)
37+
}
38+
39+
headersResp = map[string]string{
40+
"Content-Type": "application/json; charset=utf-8",
41+
"Message": "👋 hey people 🌍",
42+
}
43+
44+
jsondoc := `{"message": "", "author": ""}`
45+
jsondoc, _ = sjson.Set(jsondoc, "message", "👋 Hey! What's up?")
46+
jsondoc, _ = sjson.Set(jsondoc, "author", "Bob")
47+
48+
return jsondoc, headersResp, nil
49+
//return jsondoc, headersResp , errors.New("😡 oups I did it again")
50+
}
51+
52+
// TODO: helpers: SetHeader() ...
53+
// TODO: be able to return a status code
54+
55+
/*
56+
curl -v -X POST \
57+
http://localhost:9092 \
58+
-H 'content-type: application/json' \
59+
-d '{"message": "Golang 💚 wasm"}'
60+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
curl -v -X POST \
3+
http://localhost:9092 \
4+
-H 'content-type: application/json; charset=utf-8' \
5+
-d '{"message": "Golang 💚💜 wasm", "author": "Philippe"}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
cd ../../../capsule-launcher
3+
4+
export MESSAGE="💊 Capsule is Amazing 😍"
5+
go run main.go \
6+
-wasm=../wasm_modules/registry-demo/capsule-hey/hey.wasm \
7+
-mode=http \
8+
-httpPort=9092
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
cd ../../capsule-launcher
4+
5+
export MESSAGE="💊 Capsule Rocks 🚀"
6+
go run main.go \
7+
-wasm=./tmp/hello.wasm \
8+
-url="http://localhost:4999/k33g/hello/0.0.0/hello.wasm" \
9+
-mode=http \
10+
-httpPort=9091
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
cd ../../capsule-launcher
4+
5+
export MESSAGE="💊 Capsule Rocks 🚀"
6+
go run main.go \
7+
-wasm=./tmp/hey.wasm \
8+
-url="http://localhost:4999/k33g/hey/0.0.0/hey.wasm" \
9+
-mode=http \
10+
-httpPort=9092
11+
12+

wasm_modules/registry-demo/load-run-http-hola-orange.sh

-11
This file was deleted.

wasm_modules/registry-demo/load-run-http-hola-yellow.sh

-11
This file was deleted.

wasm_modules/registry-demo/notes.txt

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
curl -X POST http://localhost:4999/upload/k33g/hello/0.0.0 \
4+
-F "file=@./capsule-hello/hello.wasm" \
5+
-F "info=hello function from @k33g" \
6+
-H "Content-Type: multipart/form-data"
7+
8+
curl -X POST http://localhost:4999/upload/k33g/hey/0.0.0 \
9+
-F "file=@./capsule-hey/hey.wasm" \
10+
-F "info=hello hey from @k33g" \
11+
-H "Content-Type: multipart/form-data"

wasm_modules/registry-demo/publish.sh

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello function from @k33g

0 commit comments

Comments
 (0)