From 3b92a42cf1a4a7b0f7cbdb1b0d2aaa560592bdb2 Mon Sep 17 00:00:00 2001 From: Christoph Voigt Date: Mon, 14 Aug 2023 13:39:15 +0200 Subject: [PATCH] wip --- README.md | 2 +- kits/zig/worker/.gitignore | 4 ++- kits/zig/worker/README.md | 40 +++++++++++++++++++++++++++++- kits/zig/worker/build.zig | 1 + kits/zig/worker/examples/basic.zig | 24 +++++++++++++++--- kits/zig/worker/examples/main.zig | 1 + 6 files changed, 65 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c71c3540..2aacf6e9 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ Wasm Workers Server focuses on simplicity. We want you to run workers (written i | Go | ✅ | No | [#95](https://github.com/vmware-labs/wasm-workers-server/issues/95) | | Ruby | ✅ | [Yes](https://workers.wasmlabs.dev/docs/languages/ruby#installation) | [#63](https://github.com/vmware-labs/wasm-workers-server/issues/63) | | Python | ✅ | [Yes](https://workers.wasmlabs.dev/docs/languages/python#installation) | [#63](https://github.com/vmware-labs/wasm-workers-server/issues/63) | -| Zig | 🚧 | No | [#144](https://github.com/vmware-labs/wasm-workers-server/issues/144) | +| Zig | ✅ | Yes | [#144](https://github.com/vmware-labs/wasm-workers-server/issues/144) | | PHP | 🚧 | No | [#100](https://github.com/vmware-labs/wasm-workers-server/issues/100) | To get more information about multi-language support in Wasm Workers Server, [check our documentation](https://workers.wasmlabs.dev/docs/languages/introduction). diff --git a/kits/zig/worker/.gitignore b/kits/zig/worker/.gitignore index 4a0641ed..8efc5ecd 100644 --- a/kits/zig/worker/.gitignore +++ b/kits/zig/worker/.gitignore @@ -1,2 +1,4 @@ zig-cache/ -zig-out/ \ No newline at end of file +zig-out/ +examples/json.zig +examples/output.zig \ No newline at end of file diff --git a/kits/zig/worker/README.md b/kits/zig/worker/README.md index f20fd8a5..e673fb34 100644 --- a/kits/zig/worker/README.md +++ b/kits/zig/worker/README.md @@ -2,10 +2,48 @@ This folder contains the Zig kit or SDK for Wasm Workers Server. Currently, it uses the regular STDIN / STDOUT approach to receive the request and provide the response. +> *Note: this assumes Zig `0.11.0`* + ## build -To build example in ./examples +To build all example in ./examples +```bash +$ zig build -Dtarget="wasm32-wasi" ``` + +To build a specific example + +```bash +$ zig build-exe examples/.zig -target wasm32-wasi +``` + +## testing + +from `./kits/zig/worker` execute + +```bash $ zig build -Dtarget="wasm32-wasi" +$ wws ./zig-out/bin/ +``` + +## sockaddr issue + +Using `*http.Server.Response` was unsuccessful and lead to following error: + +``` +$ worker git:(144_-_add_support_for_zig) ✗ zig build -Dtarget="wasm32-wasi" +zig build-exe main Debug wasm32-wasi: error: the following command failed with 1 compilation errors: +/Users/c.voigt/.asdf/installs/zig/0.11.0/zig build-exe /Users/c.voigt/go/src/github.com/voigt/wasm-workers-server/kits/zig/worker/examples/main.zig --cache-dir /Users/c.voigt/go/src/github.com/voigt/wasm-workers-server/kits/zig/worker/zig-cache --global-cache-dir /Users/c.voigt/.cache/zig --name main -target wasm32-wasi -mcpu generic --mod worker::/Users/c.voigt/go/src/github.com/voigt/wasm-workers-server/kits/zig/worker/src/worker.zig --deps worker --listen=- +Build Summary: 6/9 steps succeeded; 1 failed (disable with --summary none) +install transitive failure +└─ install main transitive failure + └─ zig build-exe main Debug wasm32-wasi 1 errors +/Users/c.voigt/.asdf/installs/zig/0.11.0/lib/std/os.zig:182:28: error: root struct of file 'os.wasi' has no member named 'sockaddr' +pub const sockaddr = system.sockaddr; + ~~~~~~^~~~~~~~~ +referenced by: + Address: /Users/c.voigt/.asdf/installs/zig/0.11.0/lib/std/net.zig:18:12 + Address: /Users/c.voigt/.asdf/installs/zig/0.11.0/lib/std/net.zig:17:28 + remaining reference traces hidden; use '-freference-trace' to see all reference traces ``` \ No newline at end of file diff --git a/kits/zig/worker/build.zig b/kits/zig/worker/build.zig index 3bfedee8..40062d2f 100644 --- a/kits/zig/worker/build.zig +++ b/kits/zig/worker/build.zig @@ -4,6 +4,7 @@ const package_name = "worker"; const package_path = "src/worker.zig"; const examples = [2][]const u8{ "main", "basic" }; +// const examples = [4][]const u8{ "main", "basic", "json", "output" }; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{ }); diff --git a/kits/zig/worker/examples/basic.zig b/kits/zig/worker/examples/basic.zig index 4590b2ef..b3d0955e 100644 --- a/kits/zig/worker/examples/basic.zig +++ b/kits/zig/worker/examples/basic.zig @@ -1,7 +1,23 @@ const std = @import("std"); -const w = @import("worker"); +const io = std.io; +const http = std.http; +const worker = @import("worker"); + +var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); +const allocator = arena.allocator(); + +// Not working with *http.Server.Response +// fn cool(resp: *http.Server.Response, r: *http.Client.Request) void { +fn requestFn(resp: *worker.Response, r: *http.Client.Request) void { + _ = r; + std.debug.print("Hello from function\n", .{ }); + + _ = &resp.httpHeader.append("content-type", "text/plain"); + _ = &resp.httpHeader.append("x-generated-by", "wasm-workers-server"); + + _ = &resp.writeAll("Zig World!"); +} pub fn main() !void { - var word = w.GetWord(); - std.debug.print("Hello from {s}!\n", .{ word }); -} \ No newline at end of file + worker.ServeFunc(requestFn); +} diff --git a/kits/zig/worker/examples/main.zig b/kits/zig/worker/examples/main.zig index 8f70f42c..f5bdd342 100644 --- a/kits/zig/worker/examples/main.zig +++ b/kits/zig/worker/examples/main.zig @@ -12,6 +12,7 @@ fn requestFn(resp: *worker.Response, r: *http.Client.Request) void { _ = r; std.debug.print("Hello from function\n", .{ }); + // TODO: prepare to read request body and send it back // std.debug.print("+++ doing payload \n", .{ }); // var payload: []const u8 = ""; // var reqBody = r.reader().readAllAlloc(allocator, 8192) catch unreachable;