From 79e0f98959344c2730af7e9428e4c2e6287b3391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Fern=C3=A1ndez=20L=C3=B3pez?= Date: Mon, 11 Sep 2023 12:48:55 +0200 Subject: [PATCH] feat: add Zig environment variables example --- docs/docs/features/environment-variables.md | 2 +- docs/docs/languages/zig.md | 2 +- examples/zig-envs/README.md | 33 +++++++++++++++++++++ examples/zig-envs/build.zig | 26 ++++++++++++++++ examples/zig-envs/src/envs.zig | 25 ++++++++++++++++ examples/zig-envs/zig-out/bin/envs.toml | 5 ++++ 6 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 examples/zig-envs/README.md create mode 100644 examples/zig-envs/build.zig create mode 100644 examples/zig-envs/src/envs.zig create mode 100644 examples/zig-envs/zig-out/bin/envs.toml diff --git a/docs/docs/features/environment-variables.md b/docs/docs/features/environment-variables.md index fea3fdfd..0f19bc83 100644 --- a/docs/docs/features/environment-variables.md +++ b/docs/docs/features/environment-variables.md @@ -50,4 +50,4 @@ This feature allows you to configure environment variables dynamically. | Go | ✅ | | Ruby | ✅ | | Python | ✅ | -| Zig | ❌ | +| Zig | ✅ | diff --git a/docs/docs/languages/zig.md b/docs/docs/languages/zig.md index c1a97eed..bab6a48c 100644 --- a/docs/docs/languages/zig.md +++ b/docs/docs/languages/zig.md @@ -342,4 +342,4 @@ The Zig kit was originally authored by Christoph Voigt ([@voigt](https://github. | [K/V Store](../features/key-value.md) | [Environment Variables](../features/environment-variables.md) | [Dynamic Routes](../features/dynamic-routes.md) | [Folders](../features/mount-folders.md) | [HTTP Requests](../features/http-requests.md) | | --- | --- | --- | --- | --- | -| ✅ | ❌ | ✅ | ❓ | ❌ | +| ✅ | ✅ | ✅ | ❓ | ❌ | diff --git a/examples/zig-envs/README.md b/examples/zig-envs/README.md new file mode 100644 index 00000000..cdbca6a9 --- /dev/null +++ b/examples/zig-envs/README.md @@ -0,0 +1,33 @@ +# Zig environment variables example + +Compile a Zig worker to WebAssembly and run it in Wasm Workers Server. + +## Prerequisites + +* Wasm Workers Server (wws): + + ```shell-session + curl -fsSL https://workers.wasmlabs.dev/install | bash + ``` + +* [Zig](https://ziglang.org/download/) `0.11.0` + +## Build + +All specific build configurations are in `build.zig` file. + +```shell-session +zig build +``` + +## Run + +```shell-session +wws ./zig-out/bin/ +``` + +## Resources + +* [Environment variables](https://workers.wasmlabs.dev/docs/features/environment-variables) +* [Zig documentation](https://workers.wasmlabs.dev/docs/languages/zig) +* [Announcing Zig support for Wasm Workers Server](https://wasmlabs.dev/articles/Zig-support-on-wasm-workers-server/) diff --git a/examples/zig-envs/build.zig b/examples/zig-envs/build.zig new file mode 100644 index 00000000..67d8478a --- /dev/null +++ b/examples/zig-envs/build.zig @@ -0,0 +1,26 @@ +const std = @import("std"); + +const examples = [1][]const u8{ "envs" }; + +pub fn build(b: *std.Build) !void { + const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = "wasm32-wasi" }); + const optimize = b.standardOptimizeOption(.{}); + + const worker_module = b.createModule(.{ + .source_file = .{ .path = "../../kits/zig/worker/src/worker.zig" }, + }); + + inline for (examples) |example| { + const exe = b.addExecutable(.{ + .name = example, + .root_source_file = .{ .path = "src/" ++ example ++ ".zig" }, + .target = target, + .optimize = optimize, + }); + + exe.wasi_exec_model = .reactor; + exe.addModule("worker", worker_module); + + b.installArtifact(exe); + } +} diff --git a/examples/zig-envs/src/envs.zig b/examples/zig-envs/src/envs.zig new file mode 100644 index 00000000..6be4131e --- /dev/null +++ b/examples/zig-envs/src/envs.zig @@ -0,0 +1,25 @@ +const std = @import("std"); +const worker = @import("worker"); + +var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); +const allocator = arena.allocator(); + +fn requestFn(resp: *worker.Response, r: *worker.Request) void { + _ = r; + + const envvar = std.process.getEnvVarOwned(allocator, "MESSAGE") catch ""; + defer allocator.free(envvar); + + const s = + \\The environment variable value is: {s} + ; + + var body = std.fmt.allocPrint(allocator, s, .{ envvar }) catch undefined; // add useragent + + _ = &resp.headers.append("x-generated-by", "wasm-workers-server"); + _ = &resp.writeAll(body); +} + +pub fn main() !void { + worker.ServeFunc(requestFn); +} diff --git a/examples/zig-envs/zig-out/bin/envs.toml b/examples/zig-envs/zig-out/bin/envs.toml new file mode 100644 index 00000000..4c90dfc5 --- /dev/null +++ b/examples/zig-envs/zig-out/bin/envs.toml @@ -0,0 +1,5 @@ +name = "envs" +version = "1" + +[vars] +MESSAGE = "Hello! This message comes from an environment variable" \ No newline at end of file