diff --git a/docs/docs/features/environment-variables.md b/docs/docs/features/environment-variables.md index f6f9917..e8be276 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 e084b0c..3de5d32 100644 --- a/docs/docs/languages/zig.md +++ b/docs/docs/languages/zig.md @@ -32,19 +32,19 @@ In this example, the worker will get a request and print all the related informa ```c title="worker.zig" const std = @import("std"); const worker = @import("worker"); - + fn requestFn(resp: *worker.Response, r: *worker.Request) void { _ = r; - + _ = &resp.headers.append("x-generated-by", "wasm-workers-server"); _ = &resp.writeAll("hello from zig"); } - + pub fn main() !void { worker.ServeFunc(requestFn); } ``` - + 4. Additionally, you can now go further add all the information from the received `worker.Request`: ```c title="worker.zig" @@ -100,7 +100,7 @@ In this example, the worker will get a request and print all the related informa \\ ; - var body = std.fmt.allocPrint(allocator, s, .{ r.url.path, r.method, "-", payload }) catch undefined; + var body = std.fmt.allocPrint(allocator, s, .{ r.url.path, r.method, "-", payload }) catch undefined; _ = &resp.headers.append("x-generated-by", "wasm-workers-server"); _ = &resp.writeAll(body); @@ -121,7 +121,7 @@ In this example, the worker will get a request and print all the related informa --mod worker::lib/worker.zig \ --deps worker ``` - + You can also use a build script to build the project with a simple `zig build`, please find some inspiration in our [zig examples](https://github.com/vmware-labs/wasm-workers-server/tree/main/examples/). 6. Run your worker with `wws`. If you didn't download the `wws` server yet, check our [Getting Started](../get-started/quickstart.md) guide. @@ -220,7 +220,7 @@ To add a KV store to your worker, follow these steps: --mod worker::lib/worker.zig \ --deps worker ``` - + You can also use a build script to build the project with a simple `zig build`, please find some inspiration in our [zig examples](https://github.com/vmware-labs/wasm-workers-server/tree/main/examples/). 1. Create a `worker-kv.toml` file with the following content. Note the name of the TOML file must match the name of the worker. In this case we have `worker-kv.wasm` and `worker-kv.toml` in the same folder: @@ -258,10 +258,10 @@ You can define [dynamic routes by adding route parameters to your worker files]( ```c title="main.zig" const std = @import("std"); const worker = @import("worker"); - + fn requestFn(resp: *worker.Response, r: *worker.Request) void { var params = r.context.params; - + ... } ``` @@ -271,31 +271,31 @@ You can define [dynamic routes by adding route parameters to your worker files]( ```c title="main.zig" 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 { var params = r.context.params; - + var id: []const u8 = "the value is not available"; - + var v = params.get("id"); - + if (v) |val| { id = val; } - + const s = \\Hey! The parameter is: {s} ; - + var body = std.fmt.allocPrint(allocator, s, .{ id }) catch undefined; // add useragent - + _ = &resp.headers.append("x-generated-by", "wasm-workers-server"); _ = &resp.writeAll(body); } - + pub fn main() !void { worker.ServeFunc(requestFn); } @@ -318,7 +318,7 @@ You can define [dynamic routes by adding route parameters to your worker files]( ```shell-session wws . - + ⚙️ Loading routes from: . 🗺 Detected routes: - http://127.0.0.1:8080/[id] @@ -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 0000000..cdbca6a --- /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 0000000..67d8478 --- /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 0000000..6be4131 --- /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 0000000..4c90dfc --- /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