The platform · settings
Settings from the environment
Secrets, and whatever differs between machines, come from environment variables, as in a twelve-factor app. This page covers reading them, the app's public URL, and doing something only on a developer's machine. Using secrets (signing links, sealing data) is on the crypto page.
Settings: Env#
## Where the app is, for a link in an email: `--public-url` or `PUBLIC_URL` when the server has
## one, else where it listens. Never a request's `Host`, which whoever asks writes.
origin! : {} => Str
origin! = |{}| Env.public_url!({})
## A secret from the server's environment, or none.
signing_key! : {} => Try(Str, [NotFound])
signing_key! = |{}| Env.var!("APP_SECRET")
## Signing in as anyone, with no email: on a server started with `--dev` only.
dev_sign_in! : Str => Server.Response
dev_sign_in! = |member|
if (Env.dev!({})) {
Server.with_cookie(Server.redirect("/"), "member", member)
} else {
Server.refused(404, "No page here.")
}Env.var!(name)reads a server environment variable:Ok(value)orErr(NotFound). It works the same in a request, a job or a schedule, so read a secret where you need it.- The file
--env-filenames is read into the server's environment at start, before anything else:Env.var!and every plugin see itsANTHROPIC_API_KEY=sk-ant-…as if the shell had exported it.rocx devnames the.envin its folder. Without the flag no file is read, so test, CI and production servers have only the environment they were given. A variable already in the environment keeps its value. Lines areNAME=value(orexport NAME=value), with#comments and quoted values; any other line stops the server, saying which. The log names what it set, never a value. Keep the file out of git. Env.public_url!({})is where the app is, for a link in an email or another server's callback:--public-url(orPUBLIC_URL) if given, such as the proxy in front (https://status.example.com). Otherwise, where the server listens, with the port it bound (even one picked for--port 0). Never a request'sHostheader, which anyone can write.
The platform's own settings are flags, each with an environment variable: DATABASE_URL, PORT, MAX_BODY and the rest (running it). A package's settings are read by the app or its plugin: STORAGE_URL for the storage package, SMTP_URL for email. A plugin that needs a variable says so, and the server won't start without it.
Read once: the app's context#
Read a setting every request needs, and that never changes, once. The app's init! runs at start (after the migrations, so it may read the database) and makes the app's context. The host gives it to every request's middleware, actor turn and plugin call. If init! returns an Err, the server stops with that message:
program = App.program({
init!: |_started|
match Env.var!("APP_SECRET") {
Ok(secret) => Ok({ secret })
Err(NotFound) => Err("APP_SECRET isn't set")
},
})init! gets the text of the --context-file (empty without one): a way to hand the app facts at start that aren't environment variables. This site passes its build's facts that way. How middleware hands the context to pages and endpoints is on the routing page.
Only while developing#
Env.dev!({}) says whether the server runs with --dev, as rocx dev and just dev start it. Use it for what only a developer's machine should do, like the "sign in as anyone" shortcut above. Under --dev the server also reloads open pages on restart, shows a crash's reason in the answer, and, with the email plugin and no SMTP_URL, keeps outgoing email at /__rocx/mail (the dev mailbox). Env.debug!({}) says whether it runs with --debug, as tests start it; the email plugin keeps mail for the tests then too. With neither flag, as deployed, a server with the email plugin and no SMTP_URL won't start rather than lose email; nor will one whose app or plugins have tables but no database.