Skip to content

Commit

Permalink
Enhance sample
Browse files Browse the repository at this point in the history
  • Loading branch information
rstropek committed Nov 15, 2022
1 parent 54393f0 commit 2466bee
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 0 deletions.
1 change: 1 addition & 0 deletions fun_with_trees/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wabt
10 changes: 10 additions & 0 deletions fun_with_trees/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"program": "${workspaceFolder}/fractal_tree_csharp_host/bin/Debug/net6.0/fractal_tree_csharp_host.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"type": "lldb",
"request": "launch",
Expand Down
6 changes: 6 additions & 0 deletions fun_with_trees/AKS/kube.azcli
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kubectl get nodes -o wide
kubectl describe node aks-mywasipool-41740949-vmss000000
kubectl describe node aks-mywasipool-41740949-vmss000000 | grep spin

cd AKS/spin-workload
kubectl apply -f spin.yaml
8 changes: 8 additions & 0 deletions fun_with_trees/AKS/runtimeclass/wasm-runtimeclass.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: node.k8s.io/v1
kind: RuntimeClass
metadata:
name: "wasmtime-spin-v1"
handler: "spin"
scheduling:
nodeSelector:
"kubernetes.azure.com/wasmtime-spin-v1": "true"
32 changes: 32 additions & 0 deletions fun_with_trees/AKS/spin-workload/spin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: wasm-spin
spec:
replicas: 1
selector:
matchLabels:
app: wasm-spin
template:
metadata:
labels:
app: wasm-spin
spec:
runtimeClassName: wasmtime-spin-v1
containers:
- name: testwasm
image: docker.io/rstropek/fractal_tree_spin:latest
command: ["/"]
---
apiVersion: v1
kind: Service
metadata:
name: wasm-spin
spec:
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: wasm-spin
2 changes: 2 additions & 0 deletions fun_with_trees/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[workspace]
members = [
"fractal_tree",
"fractal_tree_wasm_lib",
"fractal_tree_cli",
"fractal_tree_spin",
]
3 changes: 3 additions & 0 deletions fun_with_trees/Container/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM scratch
COPY ./fractal_tree_spin.wasm .
COPY ./spin.toml .
14 changes: 14 additions & 0 deletions fun_with_trees/Container/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spin_version = "1"
authors = ["Rainer <[email protected]>"]
description = "Factal Tree with Spin"
name = "fractal_tree_spin"
trigger = { type = "http", base = "/" }
version = "0.1.0"

[[component]]
id = "fractal-tree-spin"
source = "fractal_tree_spin.wasm"
[component.trigger]
route = "/..."
[component.build]
command = "cargo build --target wasm32-wasi --release"
2 changes: 2 additions & 0 deletions fun_with_trees/fractal_tree_spin/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-wasi"
1 change: 1 addition & 0 deletions fun_with_trees/fractal_tree_spin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
22 changes: 22 additions & 0 deletions fun_with_trees/fractal_tree_spin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "fractal-tree-spin"
authors = ["Rainer <[email protected]>"]
description = "Factal Tree with Spin"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = [ "cdylib" ]

[dependencies]
# Useful crate to handle errors.
anyhow = "1"
# Crate to simplify working with bytes.
bytes = "1"
# General-purpose crate with common HTTP types.
http = "0.2"
# The Spin SDK.
spin-sdk = { git = "https://github.com/fermyon/spin", tag = "v0.6.0" }
# Crate that generates Rust Wasm bindings from a WebAssembly interface.
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "cb871cfa1ee460b51eb1d144b175b9aab9c50aba" }
fractal_tree = { path = "../fractal_tree", default-features = false, features = ["svg"] }
14 changes: 14 additions & 0 deletions fun_with_trees/fractal_tree_spin/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spin_version = "1"
authors = ["Rainer <[email protected]>"]
description = "Factal Tree with Spin"
name = "fractal_tree_spin"
trigger = { type = "http", base = "/" }
version = "0.1.0"

[[component]]
id = "fractal-tree-spin"
source = "../target/wasm32-wasi/release/fractal_tree_spin.wasm"
[component.trigger]
route = "/..."
[component.build]
command = "cargo build --target wasm32-wasi --release"
19 changes: 19 additions & 0 deletions fun_with_trees/fractal_tree_spin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anyhow::Result;
use spin_sdk::{
http::{Request, Response},
http_component,
};

mod parameters;

#[http_component]
fn fractal_tree_spin(_req: Request) -> Result<Response> {
let params = parameters::extract_parameters(&_req);

let buffer = fractal_tree::tree_svg(params.seed, params.depth, params.trunk_length);

Ok(http::Response::builder()
.status(200)
.header("content-type", "image/svg+xml")
.body(Some(buffer.into()))?)
}
39 changes: 39 additions & 0 deletions fun_with_trees/fractal_tree_spin/src/parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use spin_sdk::{
http::Request,
};

pub struct Parameters {
pub seed: String,
pub depth: usize,
pub trunk_length: f64,
}

impl Default for Parameters {
fn default() -> Self {
Self {
seed: "Hello WASM".to_string(),
depth: 13usize,
trunk_length: 100f64,
}
}
}

pub fn extract_parameters(req: &Request) -> Parameters {
let query = req.uri().query().unwrap_or("");
let mut params = Parameters::default();

for pair in query.split('&').filter(|s| !s.is_empty()) {
let mut parts = pair.split('=');
let key = parts.next().unwrap();
let value = parts.next().unwrap();

match key {
"seed" => params.seed = value.to_string(),
"depth" => params.depth = value.parse().unwrap_or(params.depth),
"trunk" => params.trunk_length = value.parse().unwrap_or(params.trunk_length),
_ => {},
}
}

params
}
25 changes: 25 additions & 0 deletions fun_with_trees/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,28 @@ run-wasi-param TARGET_FILE DEPTH TRUNK_LENGTH: (build-wasi)
run-linux: (run-linux-param "tree.svg" "13" "100")
run-linux-param TARGET_FILE DEPTH TRUNK_LENGTH: (run "fractal_tree_cli" "--target=x86_64-unknown-linux-gnu" TARGET_FILE DEPTH TRUNK_LENGTH)
help-linux: (help "fractal_tree_cli" "--target=x86_64-unknown-linux-gnu")

build-spin:
cd fractal_tree_spin; spin build
run-spin: (build-spin)
cd fractal_tree_spin; spin up
deploy-spin: (build-spin)
cd fractal_tree_spin; spin deploy

run-wasmedge: (run-wasmedge-param "./target/wasm32-wasi/debug/fractal_tree_cli.wasm" "./tree.svg" "8" "100")
run-wasmedge-stats: (run-wasmedge-param-stats "./target/wasm32-wasi/debug/fractal_tree_cli.wasm" "./tree.svg" "10" "100")
run-wasmedge-param WASM TARGET_FILE DEPTH TRUNK_LENGTH: (build-wasi)
wasmedge --dir .:. {{WASM}} -t {{TARGET_FILE}} -d {{DEPTH}} -l {{TRUNK_LENGTH}}
run-wasmedge-param-stats WASM TARGET_FILE DEPTH TRUNK_LENGTH: (build-wasi)
wasmedge --dir .:. --enable-all-statistics {{WASM}} -t {{TARGET_FILE}} -d {{DEPTH}} -l {{TRUNK_LENGTH}}
aot: (build-wasi)
wasmedgec ./target/wasm32-wasi/debug/fractal_tree_cli.wasm ./target/wasm32-wasi/debug/fractal_tree_cli_aot.wasm
run-aot:
just run-wasmedge-param "./target/wasm32-wasi/debug/fractal_tree_cli_aot.wasm" "./tree.svg" "13" "100"
run-aot-stats:
just run-wasmedge-param-stats "./target/wasm32-wasi/debug/fractal_tree_cli_aot.wasm" "./tree.svg" "13" "100"

docker: (build-wasi)
cp target/wasm32-wasi/release/fractal_tree_spin.wasm Container/
cd Container; docker build -t rstropek/fractal_tree_spin .
docker push rstropek/fractal_tree_spin:latest
7 changes: 7 additions & 0 deletions fun_with_trees/request.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
GET http://localhost:3000

###
GET http://127.0.0.1:3000/?seed=Hello%20WASM&depth=10&trunk=150

###
GET http://20.238.176.25

0 comments on commit 2466bee

Please sign in to comment.