This system allows you to define complex, nested configuration structures using environment variables. The variables are parsed into a JSON tree, making it easy to represent objects and arrays in your configuration.
-
Prefix: All environment variables must start with a specific prefix (e.g.,
STHUB), followed by double underscores (__) to indicate nesting. Example:STHUB__DATABASE__HOST=localhost -
Separator: Use double underscores (
__) to indicate nesting (object keys or array indices).
To represent nested objects, use the separator between each level:
-
STHUB__DATABASE__HOST=localhost→{ "DATABASE": { "HOST": "localhost" } } -
STHUB__DATABASE__PORT=5432→{ "DATABASE": { "PORT": "5432" } }
To represent arrays, use numeric keys as the last segment:
STHUB__SERVERS__0=alphaSTHUB__SERVERS__1=betaSTHUB__SERVERS__2=gamma→{ "SERVERS": ["alpha", "beta", "gamma"] }
If a structure mixes numeric and non-numeric keys, it will be treated as an object:
STHUB__MIXED__0=zeroSTHUB__MIXED__NAME=name_value→{ "MIXED": { "0": "zero", "NAME": "name_value" } }
If array indices are not consecutive (e.g., 0 and 2), the structure is treated as an object:
STHUB__SPARSE__0=zeroSTHUB__SPARSE__2=two→{ "SPARSE": { "0": "zero", "2": "two" } }
Given these environment variables:
STHUB__API__URL=https://example.comSTHUB__API__KEY=secretSTHUB__FEATURES__0=featureASTHUB__FEATURES__1=featureB
The resulting JSON would be:
{
"API": {
"URL": "https://example.com",
"KEY": "secret"
},
"FEATURES": ["featureA", "featureB"]
}sthub supports loading environment variables from a .env file in addition to those set in your shell. This is useful for local development, CI/CD, or when you want to avoid setting variables globally.
Add the following to your conf.yaml:
hubs:
configuration:
providers:
dotenv:
path: ".env"path: The path to your.envfile (relative to the working directory or absolute).
- On startup, sthub will load environment variables from the specified
.envfile. - Variables in
.envwill be merged with system environment variables. - The prefix and nesting rules described above apply to variables loaded from
.env.
STHUB__API__URL=https://example.com
STHUB__API__KEY=secret
STHUB__FEATURES__0=featureA
STHUB__FEATURES__1=featureB
- If both system environment variables and
.envvariables are present, system variables take precedence. - The
.envfile is loaded once at startup. Hot-reloading is planned for future versions. - You can use any valid environment variable notation supported by sthub.
This notation enables you to easily manage complex configuration hierarchies using only environment variables, making your application's configuration flexible and portable.