Configuration¶
Settings in zayt are handled in pure Python.
The settings files are located by default in the configuration directory with the
base name settings.py:
project/
├── application/
│ └── ...
└── configuration/
├── settings.py
├── settings_dev.py
└── settings_prod.py
Reading configuration values¶
The configuration values can be accessed by injecting zayt.configuration.Settings.
from typing import Annotated
from zayt.conf import Settings
from zayt.di import Inject, service
@service
class MyService:
settings: Annotated[Settings, Inject]
The Settings object is a dict like object
that can also be accessed using property syntax:
from zayt.conf import Settings
settings = Settings({"config": "value"})
assert settings["config"] == "value"
assert settings.config == "value"
Environment variables¶
The function zayt.conf.env() can be used to get environment variables and apply
type convertion of basic types (int, float, bool):
from zayt.conf import env
settings = {
"required": env("ENV_VAR"),
"optional": env("OPT_VAR", default="value"),
"int": env("INT_VAR", cast=int),
"bool": env("BOOL_VAR", cast=bool)
}
Profiles¶
Optional profiles can be activated by setting the environment variable ZAYT_PROFILE.
The framework will look for a file named settings_${ZAYT_PROFILE}.py and merge
the values with the main settings.py. Values from the profile settings take
precedence over the values from the main settings.
As an example, if we define ZAYT_PROFILE=dev, the file settings_dev.py will
be loaded. If instead we define ZAYT_PROFILE=prod, then the file settings_prod.py
will be loaded.
Multiple profiles can be activated by setting ZAYT_PROFILE with a comma separated
list of profiles, for example ZAYT_PROFILE=dev,prod. The framework will iterate
over the list and merge the settings found on each one. The precedence is from last
to first, so settings from one profile overwrite settings from the previous ones.
DotEnv¶
If running you project using zayt.run.app(), for example uvicorn zayt.run:app,
environment variables are automatically loaded from a .env file. The parsing
is done using the python-dotenv library.
By default, a .env file in the current working directory will be loaded, but it
can be customized with the environment variable ZAYT_DOTENV pointing to a .env file.