Skip to content

Commit cc57726

Browse files
author
Matthew Fisher
authored
Merge pull request krustlet#152 from bacongobbler/fix-102
feat(demos): WasCC demo for AssemblyScript
2 parents 5127f7b + 1265ee1 commit cc57726

13 files changed

Lines changed: 658 additions & 0 deletions

File tree

demos/wascc/greet/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Greet
2+
3+
An example that will greet the user with an HTTP response.
4+
5+
It is meant to be a simple demo for the wascc-provider with Krustlet.
6+
7+
## Running the example
8+
9+
This example has already been pre-built, so you only need to install it into your Kubernetes
10+
cluster.
11+
12+
Create the pod and configmap with `kubectl`:
13+
14+
```shell
15+
$ kubectl apply -f greet-wascc.yaml
16+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Hello World AssemblyScript for wasCC
2+
3+
A simple hello world example in AssemblyScript that will print the environment variables available
4+
to the process as an HTTP response.
5+
6+
It is meant to be a simple demo for the wascc-provider with Krustlet.
7+
8+
## Running the example
9+
10+
This example has already been pre-built, so you only need to install it into your Kubernetes
11+
cluster.
12+
13+
Create the pod and configmap with `kubectl`:
14+
15+
```shell
16+
$ kubectl apply -f k8s.yaml
17+
```
18+
19+
## Building from Source
20+
21+
If you want to compile the demo and inspect it, you'll need to do the following.
22+
23+
### Prerequisites
24+
25+
You'll need `npm` installed in order to install and build the dependencies. This project is using
26+
the [as-wasi](https://github.com/jedisct1/as-wasi) dependency, which is a helpful set of wrappers
27+
around the low level WASI bindings provided in AssemblyScript.
28+
29+
You'll also need
30+
[`wapc-gql2as`](https://github.com/wapc/as-codegen/tree/feature/initial_implementation). You'll have
31+
to compile it from source.
32+
33+
If you are interested in starting your own AssemblyScript project, visit the AssemblyScript [getting
34+
started guide](https://docs.assemblyscript.org/quick-start).
35+
36+
If you don't have Krustlet with the WASI provider running locally, see the instructions in the
37+
[tutorial](../../../docs/intro/tutorial03.md) for running locally.
38+
39+
### Compiling
40+
41+
Run:
42+
43+
```shell
44+
$ npm install
45+
$ npm run codegen
46+
$ npm run asbuild
47+
```
48+
49+
### Pushing
50+
51+
Before pushing the actor module, you will need to sign it and grant it the `wascc:http_server`
52+
capability. See [the waSCC documentation](https://wascc.dev/tutorials/first-actor/sign_module/) for
53+
more information.
54+
55+
```
56+
$ wascap sign build/optimized.wasm build/optimized_signed.wasm -u module.nk -i account.nk -n "Hello World" -s
57+
```
58+
59+
Once it's signed, detailed instructions for pushing a module can be found in the
60+
[tutorial](../../../docs/intro/tutorial02.md).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.ts
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { handleCall, consoleLog, handleAbort } from "wapc-guest-as";
2+
import { Environ, EnvironEntry } from "as-wasi";
3+
import { Request, Response, ResponseBuilder, Handlers } from "./module";
4+
5+
export function _start(): void {
6+
Handlers.handleRequest(handleRequest);
7+
}
8+
9+
function handleRequest(request: Request): Response {
10+
let message = "";
11+
let env = new Environ();
12+
let all_vars: Array<EnvironEntry> = env.all();
13+
14+
for (var i = 0; i < all_vars.length - 1; i++) {
15+
message += all_vars[i].key + "=" + all_vars[i].value + "\n";
16+
}
17+
message += all_vars[all_vars.length-1].key + "=" + all_vars[all_vars.length-1].value + "\n";
18+
19+
consoleLog(message);
20+
const payload = String.UTF8.encode(message);
21+
22+
return new ResponseBuilder()
23+
.withStatusCode(200)
24+
.withStatus("OK")
25+
.withBody(payload)
26+
.build();
27+
}
28+
29+
export function __guest_call(operation_size: usize, payload_size: usize): bool {
30+
return handleCall(operation_size, payload_size);
31+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../node_modules/assemblyscript/std/assembly.json",
3+
"include": [
4+
"./**/*.ts"
5+
]
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: hello-world-wascc-assemblyscript
5+
spec:
6+
containers:
7+
- name: hello-world-wascc-assemblyscript
8+
image: webassembly.azurecr.io/hello-world-wascc-assemblyscript:v0.1.0
9+
env:
10+
- name: PORT
11+
value: "8080"
12+
ports:
13+
- containerPort: 8080
14+
nodeSelector:
15+
beta.kubernetes.io/arch: wasm32-wascc
16+
tolerations:
17+
- key: "node.kubernetes.io/network-unavailable"
18+
operator: "Exists"
19+
effect: "NoSchedule"
20+
- key: "krustlet/arch"
21+
operator: "Equal"
22+
value: "wasm32-wascc"
23+
effect: "NoExecute"

0 commit comments

Comments
 (0)