CHAPTER 7THE ENVIRONMENT19

CHAPTER 7The Environment

A cgull program is a WebAssembly module, running inside a Worker, and it can see only what the Worker passes in. This chapter covers what that is, and what isn't there.

7.1 Variables and Secrets

Settings that shouldn't be written into the program go in the Worker's variables, in wrangler.jsonc:

"vars": {
    "SITE_NAME": "Ada's Notes",
    "PAGE_SIZE": "20"
}

and secrets, such as API keys, are set with wrangler, so they never appear in a file:

$ npx wrangler secret put API_KEY

The program reads either kind with cg_env, which returns NULL for a name that isn't set:

const char *site = cg_env("SITE_NAME");
const char *size = cg_env("PAGE_SIZE");
long page_size = size ? strtol(size, NULL, 10) : 20;

Variables reach the program as environment variables, so getenv works as well. Only strings are passed; bindings such as KV namespaces are left out. cgull run doesn't read wrangler.jsonc, so give it the variables a handler needs with -e NAME=value. For cgull dev, secrets can be put in a file named .dev.vars, as API_KEY=... lines, which should not be committed.

7.2 Logging

cg_log writes a line to the Worker's logs. It formats like printf and adds the newline:

cg_log("signup from %s", cg_header(req, "CF-IPCountry"));

Under cgull dev the lines appear in the terminal. For a deployed Worker, npx wrangler tail shows them as requests arrive, and they can be kept with Workers Logs in the Cloudflare dashboard. Anything the program writes to standard error goes the same way, so fprintf(stderr, ...) works too, though without the newline added.

7.3 Static Files

Files in public/ are served by Cloudflare directly, before the program is involved: public/style.css is /style.css, and public/img/logo.png is /img/logo.png. They get the right content types and caching headers, and they cost nothing in the program's time. Only requests for paths that aren't files reach the routes.

A file and a route at the same path can't both be reached; the file wins. The directory is set by "assets" in wrangler.jsonc, and removing that line turns static files off. This book's stylesheet and fonts are served this way.

7.4 Time and Chance

time, clock_gettime and gettimeofday give the real time, in UTC. One Workers quirk: the clock does not move while the program runs, only between requests, which prevents a kind of attack that measures time. So the time is right, but it can't be used to measure how long code takes.

For random numbers, arc4random, arc4random_uniform and getentropy are cryptographically strong, and suitable for tokens and session ids. rand is not, and since every request starts afresh, it gives the same numbers every time unless seeded.

7.5 What Isn't There

Some of the standard library has nothing behind it:

The network and storage are what cgull should do next; see Section 8.5.

Exercise 7-1. Show a different greeting depending on the time of day, in UTC.

Exercise 7-2. Make a handler that answers with a random 16-character token, using arc4random_uniform.

Set in Tinos and Courier Prime. Served by a C program on Cloudflare Workers, cgull 0.1.0. Source on GitHub, BSD licensed.