-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Zig environment variables example
- Loading branch information
Showing
6 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |