Principle:1Panel dev MaxKB Configuration Management
| Knowledge Sources | |
|---|---|
| Domains | Configuration, Deployment |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
A configuration management pattern that loads application settings from either YAML files or environment variables into a singleton dict subclass with typed accessors.
Description
MaxKB's configuration management follows the Singleton + Strategy patterns. The ConfigManager loads configuration at import time, choosing between two strategies based on the `MAXKB_CONFIG_TYPE` environment variable:
- YAML mode (default) — Reads from `config.yaml`, `config.yml`, or `config_example.yml` in the project root or `/opt/maxkb/conf`
- ENV mode (`MAXKB_CONFIG_TYPE=ENV`) — Reads all `MAXKB_*` environment variables, stripping the prefix
The resulting Config (a dict subclass) is exported as the global `CONFIG` singleton from `const.py`. It provides typed accessors: `get_db_setting()` builds the Django DATABASES config with connection pool options, `get_cache_setting()` builds the Redis cache config with optional Sentinel support, and `get_admin_path()` / `get_chat_path()` return configurable API URL prefixes.
Usage
Use this principle when designing:
- Dual-source configuration (file + environment) for container vs. bare metal
- Centralized configuration with typed accessors
- Redis Sentinel-aware cache configuration
Theoretical Basis
The configuration pattern uses Strategy selection at load time:
# Abstract algorithm description (NOT real implementation)
def load_config():
config = Config()
config.update(defaults) # Built-in defaults
if os.environ.get("MAXKB_CONFIG_TYPE") == "ENV":
# Strategy 1: Environment variables
for key in os.environ:
if key.startswith("MAXKB_"):
config[key[6:]] = os.environ[key]
else:
# Strategy 2: YAML file
with open("config.yaml") as f:
yaml_config = yaml.safe_load(f)
config.update(yaml_config)
return config # Global singleton
The Config dict subclass pattern allows attribute-style access (`CONFIG.DB_HOST`) and method-style accessors (`CONFIG.get_db_setting()`) while maintaining dict semantics for iteration and key lookup.