Skip to content

Commit

Permalink
feat: add Zig environment variables example
Browse files Browse the repository at this point in the history
  • Loading branch information
ereslibre committed Sep 13, 2023
1 parent 85309ee commit 79e0f98
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/docs/features/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ This feature allows you to configure environment variables dynamically.
| Go ||
| Ruby ||
| Python ||
| Zig | |
| Zig | |
2 changes: 1 addition & 1 deletion docs/docs/languages/zig.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
| --- | --- | --- | --- | --- |
| ✅ | | ✅ | ❓ | ❌ |
| ✅ | | ✅ | ❓ | ❌ |
33 changes: 33 additions & 0 deletions examples/zig-envs/README.md
Original file line number Diff line number Diff line change
@@ -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/)
26 changes: 26 additions & 0 deletions examples/zig-envs/build.zig
Original file line number Diff line number Diff line change
@@ -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);
}
}
25 changes: 25 additions & 0 deletions examples/zig-envs/src/envs.zig
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions examples/zig-envs/zig-out/bin/envs.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = "envs"
version = "1"

[vars]
MESSAGE = "Hello! This message comes from an environment variable"

0 comments on commit 79e0f98

Please sign in to comment.