From 3693154f79922cbd00ca02b59365bee0dd761aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Fern=C3=A1ndez=20L=C3=B3pez?= Date: Mon, 25 Sep 2023 17:01:03 +0200 Subject: [PATCH] chore: update environment variables zig documentation --- docs/docs/features/environment-variables.md | 1 + docs/docs/languages/zig.md | 44 +++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/docs/docs/features/environment-variables.md b/docs/docs/features/environment-variables.md index e8be276..3223612 100644 --- a/docs/docs/features/environment-variables.md +++ b/docs/docs/features/environment-variables.md @@ -23,6 +23,7 @@ Then, you can read them in your worker: * [Read environment variables in Python](../languages/python.md#read-environment-variables) * [Read environment variables in Ruby](../languages/ruby.md#read-environment-variables) * [Read environment variables in Go](../languages/go.md#read-environment-variables) +* [Read environment variables in Zig](../languages/zig.md#read-environment-variables) ## Inject existing environment variables diff --git a/docs/docs/languages/zig.md b/docs/docs/languages/zig.md index 3de5d32..73655f3 100644 --- a/docs/docs/languages/zig.md +++ b/docs/docs/languages/zig.md @@ -328,6 +328,50 @@ You can define [dynamic routes by adding route parameters to your worker files]( 5. Finally, open in your browser. +## Read environment variables + +Environment variables are configured [via the related TOML configuration file](../features/environment-variables.md). These variables are accessible via `std.process.getEnvMap` or `std.process.getEnvVarOwned` in your worker. To read them, just use the same name you configured in your TOML file: + +```toml title="envs.toml" +name = "envs" +version = "1" + +[vars] +MESSAGE = "Hello 👋! This message comes from an environment variable" +``` + +Now, you can read the `MESSAGE` variable using either the `std.process.getEnvMap` or the `std.process.getEnvVarOwned` functions: + +```c title="envs.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 { + _ = 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); +} +``` + +If you prefer, you can configure the environment variable value dynamically by following [these instructions](../features/environment-variables.md#inject-existing-environment-variables). + ## Other examples Find other examples in the [`/examples` directory](https://github.com/vmware-labs/wasm-workers-server/tree/main/examples/) of wasm-workers-server repository.