Skip to main content

Module keys

Module keys 

Source
Expand description

The configuration keys Rainier itself reads — a typed index of the tree.

Some frameworks answer “what can I configure?” with a shipped file to read through. Rainier’s answer is this module: every key the framework writes or reads, with the type stored at it.

use rainier_framework::config::Config;
use rainier_framework::keys;

let config = Config::new();
config.set(keys::APP_NAME, "My App".to_string()).unwrap();

assert_eq!(config.get(keys::APP_NAME).as_deref(), Some("My App"));

Using them is optional — every Config method still takes a &str — but it is what turns two classes of silent mistake into compile errors:

ⓘ
// The port is a u16, so this does not compile.
config.set(keys::SERVER_PORT, "8000").unwrap();
ⓘ
// Neither does a driver spelled as a string.
config.set(keys::CACHE_DRIVER, "redis").unwrap();

§Reading a driver

The driver keys hold settings — closed sets with their own parser. Read them with Config::setting, which distinguishes unset (use the default) from set to nonsense (fail, and list what was expected):

use rainier_framework::cache::CacheDriver;
use rainier_framework::config::Config;
use rainier_framework::keys;

let config = Config::new();
config.set(keys::CACHE_DRIVER, CacheDriver::Redis).unwrap();

match config.setting(keys::CACHE_DRIVER).unwrap() {
    CacheDriver::Memory => { /* … */ }
    CacheDriver::Redis | CacheDriver::RedisCluster => { /* … */ }
    CacheDriver::Memcached => { /* … */ }
    CacheDriver::DynamoDb => { /* … */ }
    // A `_` arm is worth having: a driver added to the framework is a new
    // variant, and a new variant stops an exhaustive match compiling.
    other => panic!("this application does not build a {other}"),
}

§Adding your own

An application declares its keys the same way, next to the section that writes them:

use rainier_framework::config::config_keys;

config_keys! {
    /// How many posts a listing shows.
    pub POSTS_PER_PAGE: u64 = "posts.per_page";
}

Constants§

APP_BASE_PATH
The directory the application was started from. Set by the framework; there is no environment variable for it.
APP_CIPHER
Which encryption envelope this application writes — native or php.
APP_DEBUG
Whether a failure may show its internals to the client.
APP_ENV
Which deployment this is.
APP_NAME
The application’s display name, in page titles and mail.
APP_URL
The application’s canonical base URL, used to generate absolute links.
CACHE_DRIVER
Which cache store to build.
CACHE_MEMCACHED_URL
host:port of the Memcached server.
CACHE_PREFIX
Prepended to every cache key, so two applications can share a server.
CACHE_REDIS_URL
The Redis DSN, or a comma-separated seed list for a cluster.
DATABASE_URL
The database DSN — sqlite::memory:, mysql://…, postgres://….
KAFKA_BROADCAST_TOPIC
The topic broadcasts are published to.
KAFKA_BROKERS
The bootstrap brokers, comma-separated — kafka-1:9092,kafka-2:9092.
KAFKA_GROUP
Which set of cursors this deployment shares.
KAFKA_PASSWORD
The SASL password.
KAFKA_SASL_MECHANISM
Which SASL mechanism the username and password are for.
KAFKA_TLS
Whether to connect over TLS. Needs the kafka-tls feature.
KAFKA_TOPIC_PREFIX
Prefixes every topic this application produces to or reads from.
KAFKA_USERNAME
The SASL username, if the cluster wants one.
LOG_FORMAT
The shape of a log line — auto, pretty, compact, json.
MAIL_DRIVER
Where mail goes.
MAIL_FROM_ADDRESS
The default From address.
MAIL_FROM_NAME
The default From display name.
METRICS_ENABLED
Whether to record Prometheus metrics.
METRICS_PATH
The path the scrape endpoint is served at.
OPENAPI_ENABLED
Whether to serve the OpenAPI document.
OPENAPI_PATH
The path the document is served at.
OPENAPI_SERVER
The base URL clients should use, if the document should name one.
OPENAPI_TITLE
The info.title of the document.
OPENAPI_VERSION
The info.version of the document — your API’s version, not the framework’s.
QUEUE_DEFAULT
The queue a job goes on when it does not name one.
QUEUE_DRIVER
Where queued jobs wait.
SERVER_COMPRESSION
Whether to gzip text responses on the way out.
SERVER_HOST
The interface the HTTP server binds to.
SERVER_MAX_BODY_BYTES
The largest request body that will be buffered, in bytes.
SERVER_PORT
The port the HTTP server binds to.
SERVER_REQUEST_TIMEOUT_SECS
How long a handler may take before the request is cancelled and answered 408, in seconds. 0 turns it off.
SESSION_COOKIE
The name of the session cookie.
SESSION_DRIVER
Where session state lives.
SESSION_LIFETIME
How long a session survives without a request, in seconds.
SESSION_SECURE
Whether the session cookie is Secure. Must be true over HTTPS.
TELEMETRY_ENABLED
Whether to join and propagate W3C trace context.
TELEMETRY_ENDPOINT
The OTLP collector’s gRPC endpoint — http://localhost:4317.
TELEMETRY_SAMPLE_RATIO
What fraction of traces this service starts to record, 0.0 to 1.0.
TELEMETRY_SERVICE_NAME
What this service calls itself in a trace.