From fd84699bdcbe67bf4cd6ae9feb18e57b93653bde Mon Sep 17 00:00:00 2001 From: k33g Date: Fri, 11 Aug 2023 09:06:28 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20updates=20HDK=20for=20v0.4.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- capsule-cli/functions/redis-db/build.sh | 12 +++++++ capsule-cli/functions/redis-db/main.go | 1 + capsule-cli/functions/redis-db/run.sh | 4 +++ .../functions/hello-redis-web/.gitignore | 1 + .../functions/hello-redis-web/build.sh | 12 +++++++ capsule-http/functions/hello-redis-web/go.mod | 9 +++++ capsule-http/functions/hello-redis-web/go.sum | 2 ++ .../functions/hello-redis-web/main.go | 34 +++++++++++++++++++ .../functions/hello-redis-web/start.sh | 3 ++ 9 files changed, 78 insertions(+) create mode 100755 capsule-cli/functions/redis-db/build.sh create mode 100755 capsule-cli/functions/redis-db/run.sh create mode 100644 capsule-http/functions/hello-redis-web/.gitignore create mode 100755 capsule-http/functions/hello-redis-web/build.sh create mode 100644 capsule-http/functions/hello-redis-web/go.mod create mode 100644 capsule-http/functions/hello-redis-web/go.sum create mode 100644 capsule-http/functions/hello-redis-web/main.go create mode 100755 capsule-http/functions/hello-redis-web/start.sh diff --git a/capsule-cli/functions/redis-db/build.sh b/capsule-cli/functions/redis-db/build.sh new file mode 100755 index 0000000..405b02e --- /dev/null +++ b/capsule-cli/functions/redis-db/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash +tinygo build -o redis-db.wasm \ + -scheduler=none \ + --no-debug \ + -target wasi ./main.go +ls -lh *.wasm + +echo "📦 Building capsule-cli..." +cd ../.. +go build -ldflags="-s -w" -o capsule +ls -lh capsule +mv capsule ./functions/redis-db diff --git a/capsule-cli/functions/redis-db/main.go b/capsule-cli/functions/redis-db/main.go index d563253..648f59e 100644 --- a/capsule-cli/functions/redis-db/main.go +++ b/capsule-cli/functions/redis-db/main.go @@ -12,6 +12,7 @@ func main() { // Handle function func Handle(params []byte) ([]byte, error) { + capsule.RedisSet("one", []byte("👋")) capsule.RedisSet("one", []byte("👋")) capsule.RedisSet("two", []byte("hello")) capsule.RedisSet("three", []byte("world")) diff --git a/capsule-cli/functions/redis-db/run.sh b/capsule-cli/functions/redis-db/run.sh new file mode 100755 index 0000000..1247104 --- /dev/null +++ b/capsule-cli/functions/redis-db/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash +REDIS_URI="" \ +./capsule --wasm=./redis-db.wasm + diff --git a/capsule-http/functions/hello-redis-web/.gitignore b/capsule-http/functions/hello-redis-web/.gitignore new file mode 100644 index 0000000..9257d91 --- /dev/null +++ b/capsule-http/functions/hello-redis-web/.gitignore @@ -0,0 +1 @@ +capsule-http-* diff --git a/capsule-http/functions/hello-redis-web/build.sh b/capsule-http/functions/hello-redis-web/build.sh new file mode 100755 index 0000000..cc28c7d --- /dev/null +++ b/capsule-http/functions/hello-redis-web/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash +tinygo build -o hello-redis-web.wasm \ + -scheduler=none \ + --no-debug \ + -target wasi ./main.go +ls -lh *.wasm + +echo "📦 Building capsule-http..." +cd ../.. +go build -ldflags="-s -w" -o capsule-http +ls -lh capsule-http +mv capsule-http ./functions/hello-redis-web \ No newline at end of file diff --git a/capsule-http/functions/hello-redis-web/go.mod b/capsule-http/functions/hello-redis-web/go.mod new file mode 100644 index 0000000..8543f0f --- /dev/null +++ b/capsule-http/functions/hello-redis-web/go.mod @@ -0,0 +1,9 @@ +module hello-redis-web + +go 1.20 + +require github.com/bots-garden/capsule-module-sdk v0.0.6 + +require github.com/valyala/fastjson v1.6.4 // indirect + +replace github.com/bots-garden/capsule-module-sdk => ../../../../capsule-module-sdk diff --git a/capsule-http/functions/hello-redis-web/go.sum b/capsule-http/functions/hello-redis-web/go.sum new file mode 100644 index 0000000..d0e4603 --- /dev/null +++ b/capsule-http/functions/hello-redis-web/go.sum @@ -0,0 +1,2 @@ +github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ= +github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY= diff --git a/capsule-http/functions/hello-redis-web/main.go b/capsule-http/functions/hello-redis-web/main.go new file mode 100644 index 0000000..96cc445 --- /dev/null +++ b/capsule-http/functions/hello-redis-web/main.go @@ -0,0 +1,34 @@ +// Package main +package main + +import ( + //"errors" + + "errors" + + "github.com/bots-garden/capsule-module-sdk" +) + +func main() { + + capsule.SetHandleHTTP(func(param capsule.HTTPRequest) (capsule.HTTPResponse, error) { + + // get values from Redis + message, errMsg := capsule.RedisGet("message") + xHandle, errHandle := capsule.RedisGet("x_handle") + errs := errors.Join(errMsg, errHandle) + + if errs != nil { + capsule.Log("😡 " + errs.Error()) + } + + response := capsule.HTTPResponse{ + JSONBody: `{"message": "` + string(message) + `", "xHandle": "` + string(xHandle) + `"}`, + Headers: `{"Content-Type": "application/json; charset=utf-8"}`, + StatusCode: 200, + } + + return response, errs + + }) +} diff --git a/capsule-http/functions/hello-redis-web/start.sh b/capsule-http/functions/hello-redis-web/start.sh new file mode 100755 index 0000000..10814fd --- /dev/null +++ b/capsule-http/functions/hello-redis-web/start.sh @@ -0,0 +1,3 @@ +#!/bin/bash +REDIS_URI="" \ +./capsule-http --wasm=./hello-redis-web.wasm --httpPort=8080