Skip to content

Commit

Permalink
feat(examples): Migrates remaining go examples from monorepo
Browse files Browse the repository at this point in the history
This moves the two examples that weren't moved from the monorepo. This
also adds tests and adds simple smoke tests for each component. I also
modified the examples job to run once per week to ensure things don't go
stale with new versions of `wash`

Signed-off-by: Taylor Thomas <[email protected]>
  • Loading branch information
thomastaylor312 committed Dec 18, 2024
1 parent e8b3f29 commit 8e1f3a4
Show file tree
Hide file tree
Showing 434 changed files with 31,690 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/component-go.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Go

on:
# We trigger this workflow once a week on Saturdays to make sure the examples don't get stale
schedule:
- cron: "0 0 * * 6"
push:
branches: ["main"]
paths:
Expand Down Expand Up @@ -84,7 +87,9 @@ jobs:
example:
- http-server
- http-client
- http-password-checker
- invoke
- sqldb-postgres-query
tinygo-version:
- "0.33.0"
- "0.34.0"
Expand Down Expand Up @@ -118,3 +123,32 @@ jobs:
working-directory: "./examples/component/${{ matrix.example }}"
run: |
wash build
- name: run tests
working-directory: "./examples/component/${{ matrix.example }}"
run: go test .

# Run the wadm file and make sure it deploys
- name: test component load
shell: bash
working-directory: "./examples/component/${{ matrix.example }}"
# TODO: Add a test to the matrix for testing the running component (i.e. with `curl` or `wash call`)
run: |
set -xe
wash up &
WASH_PID=$!
sleep 4;
wash app deploy wadm.yaml;
TRIES=0
while [[ $(wash get inventory --output=json | jq '.inventories[0].components | length') -eq 0 ]] ; do
if [[ $TRIES -gt 10 ]]; then
echo "❌ failed to find component in inventory output after deploying example manifest";
exit -1;
fi
TRIES=$((TRIES+1));
sleep 1;
done;
echo "✅ successfully started at least one component";
wash app delete wadm.yaml;
kill $WASH_PID;
exit 0;
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ go.work.sum

# env file
.env
.envrc
1 change: 1 addition & 0 deletions examples/component/http-password-checker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/*
88 changes: 88 additions & 0 deletions examples/component/http-password-checker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# TinyGo HTTP Password Checker

This repository contains a WebAssembly Component written in [TinyGo][tinygo], which:

- Implements a [`wasi:http`][wasi-http]-compliant HTTP handler
- Uses the [`httpserver` provider][httpserver-provider] to serve requests
- Can be declaratively provisioned with [`wadm`][wadm]

[wasi-http]: https://github.com/WebAssembly/wasi-http
[httpserver-provider]: https://github.com/wasmCloud/wasmCloud/tree/main/crates/provider-http-server
[wadm]: https://github.com/wasmCloud/wadm
[tinygo]: https://tinygo.org/getting-started/install/
[wash]: https://wasmcloud.com/docs/ecosystem/wash/

# Dependencies

Before starting, ensure that you have the following installed:

- The [TinyGo toolchain][tinygo]
- [`wash`, the WAsmcloud SHell][wash] installed.

# Quickstart

To get started developing this repository quickly, clone the repo and run `wash dev`:

```console
wash dev
```

`wash dev` does many things for you:

- Starts the [wasmCloud host][wasmcloud-host] that can run your WebAssembly component
- Builds this project
- Builds a declarative WADM manifest consisting of:
- Your locally built component
- A [HTTP server provider][httpserver-provider] which will receive requests from the outside world (on port 8000 by default)
- Necessary links between providers and your component so your component can handle web traffic
- Deploys the built manifest (i.e all dependencies to run this application) locally
- Watches your code for changes and re-deploys when necessary.

[wasmcloud-host]: https://wasmcloud.com/docs/concepts/hosts

## Send a request to the running component

Once `wash dev` is serving your component, to send a request to the running component (via the HTTP server provider):

```console
curl localhost:8000/api/v1/check -d '{"value": "tes12345!"}'
```

You should see a JSON response like:

```json
{
"valid": false, "message": "insecure password, try including more special characters, using uppercase letters or using a longer password"
}
```

## Adding Capabilities

To learn how to extend this example with additional capabilities, see the [Adding Capabilities](https://wasmcloud.com/docs/tour/adding-capabilities?lang=rust) section of the wasmCloud documentation.

# Issues/ FAQ

<summary>
<description>

## `curl` produces a "failed to invoke" error

</description>

If `curl`ing produces

```
➜ curl localhost:8000
failed to invoke `wrpc:http/incoming-handler.handle`: failed to invoke `wrpc:http/[email protected]`: failed to shutdown synchronous parameter channel: not connected%
```

You *may* need to just wait a little bit -- the HTTP server takes a second or two to start up.

If the issue *persists*, you *may* have a lingering HTTP server provider running on your system. You can use `pgrep` to find it:

```console
pgrep -la ghcr_io
4007604 /tmp/wasmcloudcache/NBCBQOZPJXTJEZDV2VNY32KGEMTLFVP2XJRZJ5FWEJJOXESJXXR2RO46/ghcr_io_wasmcloud_http_server_0_23_1
```

</summary>
Loading

0 comments on commit 8e1f3a4

Please sign in to comment.