Roc Solid
Markup in Roc · pages on a native server · components in WebAssembly

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#

server/test/app/Samples.roc, in part
## 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.")
	}

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:

main.roc
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.