Skip to content

Commit

Permalink
📦 updates HDK for v0.3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
k33g committed Jun 4, 2023
1 parent 311b463 commit 63a35ac
Show file tree
Hide file tree
Showing 21 changed files with 386 additions and 76 deletions.
24 changes: 24 additions & 0 deletions .gitpod.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM gitpod/workspace-base:latest

# ------------------------------------
# Install Go
# ------------------------------------
ENV GO_VERSION=1.20

ENV GOPATH=$HOME/go-packages
ENV GOROOT=$HOME/go
ENV PATH=$GOROOT/bin:$GOPATH/bin:$PATH
RUN curl -fsSL https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz | tar xzs \
&& printf '%s\n' 'export GOPATH=/workspace/go' \
'export PATH=$GOPATH/bin:$PATH' > $HOME/.bashrc.d/300-go

# ------------------------------------
# Install TinyGo
# ------------------------------------
ARG TINYGO_VERSION="0.27.0"
RUN wget https://github.com/tinygo-org/tinygo/releases/download/v${TINYGO_VERSION}/tinygo_${TINYGO_VERSION}_amd64.deb
RUN sudo dpkg -i tinygo_${TINYGO_VERSION}_amd64.deb
RUN rm tinygo_${TINYGO_VERSION}_amd64.deb

RUN go install github.com/go-task/task/v3/cmd/task@latest
RUN go install -v golang.org/x/tools/gopls@latest
2 changes: 2 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
image:
file: .gitpod.Dockerfile
10 changes: 6 additions & 4 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ tasks:
#TAG: "v0.3.5"
#TAG: "v0.3.6" # next release
#TAG: "v0.3.7" # next release (this is a pre-release)
TAG: "v0.3.8"
#TAG: "v0.3.8"
TAG: "v0.3.9"
#TAG: "v0.4.0"

cmds:
- echo "📦 Generating release..."
Expand All @@ -36,13 +38,13 @@ tasks:

remove-tag:
env:
TAG: "v0.3.8"
TAG: "v0.3.9"
cmds:
- git tag -d ${TAG}

build-releases:
env:
TAG: "v0.3.8"
TAG: "v0.3.9"
cmds:
- |
cd capsule-cli
Expand Down Expand Up @@ -75,7 +77,7 @@ tasks:
build-push-docker-image:
vars:
IMAGE_BASE_NAME: "capsule-http"
IMAGE_TAG: "0.3.8"
IMAGE_TAG: "0.3.9"
cmds:
- echo "👋 {{.IMAGE_BASE_NAME}}-{{.GOOS}}-{{.GOARCH}}:{{.IMAGE_TAG}}"
- |
Expand Down
13 changes: 12 additions & 1 deletion capsule-http/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ tasks:
GOOS: "linux"
GOARCH: "arm64"
IMAGE_BASE_NAME: "capsule-http"
IMAGE_TAG: "0.3.8"
IMAGE_TAG: "0.3.9"
cmds:
- |
IMAGE_NAME="${IMAGE_BASE_NAME}-${GOOS}-${GOARCH}"
Expand Down Expand Up @@ -365,6 +365,17 @@ tasks:
-target wasi ./main.go
ls -lh *.wasm
start-index-html:
env:
HTTP_PORT: '7777'
WASM_FILE: './functions/index-html/index.wasm'
cmds:
- |
echo "🚀 Testing hey..."
./capsule-http --wasm=${WASM_FILE} --httpPort=${HTTP_PORT}
test-index-html:
env:
DATA: 'Bob Morane'
Expand Down
2 changes: 1 addition & 1 deletion capsule-http/functions/hello-world/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM botsgarden/capsule-http-linux-arm64:0.3.8
FROM botsgarden/capsule-http-linux-arm64:0.3.9
COPY hello-world.wasm .
EXPOSE 8080
CMD ["/capsule-http", "--wasm=./hello-world.wasm", "--httpPort=8080"]
Expand Down
10 changes: 10 additions & 0 deletions capsule-http/functions/index-html/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Serving HTML with Capsule HTTP server

```bash
# build and install capsule-http
task install
# build the wasm module
task build
# serve the wasm module
task serve
```
35 changes: 35 additions & 0 deletions capsule-http/functions/index-html/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: '3'

# capsctl usages
tasks:

# build capsule-http and deploy on OVH
install:
cmds:
- |
echo "📦 Building capsule-http..."
cd ../..
pwd
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o capsule-http
ls -lh capsule-http
sudo cp capsule-http /usr/local/bin/capsule-http
capsule-http --version
build:
cmds:
- |
echo "📦 Building index.wasm module..."
tinygo build -o index.wasm \
-scheduler=none \
--no-debug \
-target wasi ./main.go
ls -lh *.wasm
serve:
env:
HTTP_PORT: '7070'
WASM_FILE: './index.wasm'
cmds:
- |
capsule-http --wasm=${WASM_FILE} --httpPort=${HTTP_PORT}
24 changes: 24 additions & 0 deletions capsule-http/functions/index-html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<head>
<meta charset="utf-8">
<title>Capsule 💜 Wasm & Wazero</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.container { min-height: 100vh; display: flex; justify-content: center; align-items: center; text-align: center; }
.title { font-family: "Source Sans Pro", "Helvetica Neue", Arial, sans-serif; display: block; font-weight: 300; font-size: 80px; color: #35495e; letter-spacing: 1px; }
.subtitle { font-family: "Source Sans Pro", "Helvetica Neue", Arial, sans-serif; font-weight: 300; font-size: 32px; color: #526488; word-spacing: 5px; padding-bottom: 15px; }
.links { padding-top: 15px; }
</style>
</head>

<body>
<section class="container">
<div>
<h1 class="title">👋 Hello World 🌍</h1>
<h2 class="subtitle">Served with 💜 by Capsule [HTTP] v0.3.9 🥒 [cucumber] 💊</h2>
<h2 class="subtitle">🎉 Happily built thanks to Wazero</h2>
</div>
</section>
</body>

</html>
34 changes: 19 additions & 15 deletions capsule-http/functions/index-html/main.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
// Package main
// Package main, this module is serving HTML
package main

import (
_ "embed"
capsule "github.com/bots-garden/capsule-module-sdk"
)

var (
//go:embed index.html
html []byte
)

func main() {
capsule.SetHandleHTTP(Handle)
capsule.SetHandleHTTP(func (param capsule.HTTPRequest) (capsule.HTTPResponse, error) {
return capsule.HTTPResponse{
TextBody: string(html),
Headers: `{
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-cache",
"X-Powered-By": "capsule-module-sdk"
}`,
StatusCode: 200,
}, nil
})
}

// Handle function
func Handle(param capsule.HTTPRequest) (capsule.HTTPResponse, error) {

return capsule.HTTPResponse{
TextBody: "<h1>👋 Hello World! 🌍</h1>",
Headers: `{
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-cache",
"X-Powered-By": "capsule-module-sdk"
}`,
StatusCode: 200,
}, nil
}

85 changes: 68 additions & 17 deletions docs/capsule-http-faas-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A Capsule HTTP server can start/spawn other Capsule HTTP server processes.
### Install the last version of Capsule HTTP

```bash
VERSION="v0.3.8" OS="linux" ARCH="arm64"
VERSION="v0.3.9" OS="linux" ARCH="arm64"
wget -O capsule-http https://github.com/bots-garden/capsule/releases/download/${VERSION}/capsule-http-${VERSION}-${OS}-${ARCH}
chmod +x capsule-http
sudo cp capsule-http /usr/local/bin/capsule-http
Expand All @@ -24,7 +24,7 @@ capsule-http --version
**CapsCtl** is a CLI to send commands to the Capsule HTTP server when it is unning in **FaaS** mode.

```bash
VERSION="v0.3.8" OS="linux" ARCH="arm64"
VERSION="v0.3.9" OS="linux" ARCH="arm64"
wget -O capsctl https://github.com/bots-garden/capsule/releases/download/${VERSION}/capsctl-${VERSION}-${OS}-${ARCH}
chmod +x capsctl
sudo cp capsctl /usr/local/bin/capsctl
Expand All @@ -35,25 +35,33 @@ capsctl --version
## Start Capsule HTTP FaaS mode

```bash
CAPSULE_DOMAIN="http://localhost" \
CAPSULE_FAAS_TOKEN="ILOVEPANDAS" \
capsule-http \
--wasm=./functions/index-page/index-page.wasm \
--httpPort=8080 \
--faas=true
```

> **Remarks:**: if you use SSL certificates, use these options:
> - `--crt=faas.capsule.foundation.crt`
> - `--key=faas.capsule.foundation.key`

You should get an output like this:
```
2023/05/29 15:12:18 🚀 faas mode activated!
2023/05/29 15:12:18 📦 wasm module loaded: ./functions/index-page/index-page.wasm
2023/05/29 15:12:18 💊 Capsule [HTTP] v0.3.8 🥬 [leafy greens]
2023/05/29 15:12:18 💊 Capsule [HTTP] v0.3.9 🥒 [cucumber]
http server is listening on: 8080 🌍
```

> In a future version, the wasm file won't be mandatory anymore.
> **Remarks:**
> - the wasm file (`--wasm`) is optional (a default message is served if not specified)
> - `CAPSULE_FAAS_TOKEN` is used to authenticate the `capsctl` CLI
## Start a function

## Launch another Capsule HTTP server
With the FaaS mode activated, you can start functions. It' like running another **Capsule HTTP** processes (one wasm function == one Capsule HTTP process).

```bash
export CAPSULE_FAAS_TOKEN="ILOVEPANDAS"
Expand All @@ -67,13 +75,14 @@ capsctl \
--revision=green \
--description="this the hello module, green revision" \
--env='["MESSAGE=🟢","GREETING=🤗"]' \
--path="/usr/local/bin/capsule-http" \
--wasm=./functions/hello-green/hello-green.wasm
```
> - `--stopAfter=10` this will stop the Capsule HTTP server process after 10 seconds
> - `--stopAfter=10` this will stop the Capsule HTTP server process after 10 seconds ()optional
> - `--stopAfter` is not mandatory (then the Capsule HTTP server process will never stop)
> - if the process is stopped, the Capsule HTTP server will be restarted at every call
> - `--path` means you can use various version of Capsule HTTP
> - if the process is stopped, the Capsule HTTP server will be restarted at next call
> - `--description=` is optional
> - `--env='["MESSAGE=🟢","GREETING=🤗"]'` allows to pass environment variables to the function (optional)
> - `--wasm`: where to find the wasm file
**Now you can use this URL `http://localhost:8080/functions/hello/green` to call the hello green function**

Expand All @@ -85,20 +94,36 @@ curl -X POST http://localhost:8080/functions/hello/green \
-d "Bob Morane"
```

## Launch another Capsule HTTP server process
### Default revision

If you don't specify a revision, the default revision is called **default**, then you can call the function like this:

```bash
curl -X POST http://localhost:8080/functions/hello \
-H 'Content-Type: text/plain; charset=utf-8' \
-d "Bob Morane"
```

Or like this:

```bash
curl -X POST http://localhost:8080/functions/hello/default \
-H 'Content-Type: text/plain; charset=utf-8' \
-d "Bob Morane"
```

> 👋 the revision concept is useful to handle several version of a wasm module/function.
## Launch another function

```bash
export CAPSULE_FAAS_TOKEN="ILOVEPANDAS"
export CAPSULE_INSTALL_PATH="/usr/local/bin/capsule-http"
export CAPSULE_MAIN_PROCESS_URL="http://localhost:8080"

capsctl \
--cmd=start \
--stopAfter=10 \
--name=hello \
--revision=blue \
--description="this the hello module, blue revision" \
--env='["MESSAGE=🔵","GREETING=🎉"]'\
--path="/usr/local/bin/capsule-http" \
--wasm=./functions/hello-blue/hello-blue.wasm
```

Expand All @@ -112,7 +137,7 @@ curl -X POST http://localhost:8080/functions/hello/blue \
-d "Bob Morane"
```

## Stop and remove a running Capsule HTTP server process
## Drop: stop and remove a running function

```bash
export CAPSULE_FAAS_TOKEN="ILOVEPANDAS"
Expand Down Expand Up @@ -149,3 +174,29 @@ curl -X POST http://localhost:8080/functions/hello/green \
-H 'Content-Type: text/plain; charset=utf-8' \
-d "Bob Morane"
```

## Download the wasm module before starting the function

You can specify to the Capsule HTTP process with the `--url` option, where to download the wasm file and where to save it before starting with the `--wasm` option:

```bash
export CAPSULE_FAAS_TOKEN="ILOVEPANDAS"
export CAPSULE_MAIN_PROCESS_URL="http://localhost:8080"

capsctl \
--cmd=start \
--name=hello \
--revision=0.0.1 \
--wasm= ./store/hello.0.0.1.wasm \
--url=http://wasm.files.com/hello/0.0.1/hello.0.0.1.wasm
```

### Authentication of the downlad

If you need to provide an authentication token, you can use these options:

```bash
--authHeaderName="PRIVATE-TOKEN" \
--authHeaderValue="${GITLAB_WASM_TOKEN}" \
```

Loading

0 comments on commit 63a35ac

Please sign in to comment.