Skip to main content

Module config

Module config 

Source
Expand description

Typed app configuration from Worker variables and secrets, and the environment (development or production).

An Ocre app has no config files at run time: its settings are the plain-text variables of cloudflare.config.ts (KEY: bindings.text("...")) and its secrets (ocre secrets push), with .dev.vars overriding both in ocre dev. Declare them once as a struct and read it with Ctx::config (Loco’s settings, Rails’ config.x and credentials):

use axum::extract::State;
use ocre::{Ctx, Result};
use serde::Deserialize;

/// Field `stripe_secret_key` reads `STRIPE_SECRET_KEY`.
#[derive(Deserialize)]
struct Settings {
    stripe_secret_key: String,
    #[serde(default = "default_page_size")]
    page_size: u32,
    support_email: Option<String>,
    beta_features: Vec<String>, // "search,exports"
}

fn default_page_size() -> u32 {
    25
}

async fn checkout(State(ctx): State<Ctx>) -> Result<String> {
    let settings: Settings = ctx.config()?;
    Ok(format!("{} per page", settings.page_size))
}

Each field reads the variable or secret named like the field in upper case (#[serde(rename = "...")] names it exactly, before upper-casing). Values are text, converted to the field’s type: numbers, bool (true/false/1/0), char, String, Vec<_> from a comma-separated list, and unit enums by name. Option fields and #[serde(default)] fields may be missing; a missing required field is Error::Internal naming the variable and the two places to set it.

§Free plan

Reading costs no request or binding operation: one environment lookup per field (microseconds of CPU). The free plan allows 64 variables and secrets per Worker, 5 KB each (September 2026).

Enums§

Environment
Where the Worker runs, from its build profile (Rails’ Rails.env, Loco’s environment).

Functions§

from_vars
Reads T from the given variables: what Ctx::config does with the Worker’s environment.