Implementation:1Panel dev MaxKB ConfigManager
| Knowledge Sources | |
|---|---|
| Domains | Configuration, Deployment |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
Concrete configuration loader that reads settings from YAML files or MAXKB_ environment variables, provided by MaxKB.
Description
The ConfigManager class and Config dict subclass provide the configuration foundation for MaxKB. ConfigManager.load_user_config() is called once at import time (in `const.py`) to produce a global CONFIG singleton. The config source is determined by `MAXKB_CONFIG_TYPE`: if set to `ENV`, it reads all `MAXKB_*` environment variables (stripping the prefix); otherwise it loads from YAML files (`config.yaml`, `config.yml`, or `config_example.yml`). The Config class provides typed accessors: `get_db_setting()` builds the Django DATABASES config, `get_cache_setting()` builds the Redis cache config (with Sentinel support), `get_admin_path()` and `get_chat_path()` return API URL prefixes.
Usage
Import this class when you need to:
- Understand how MaxKB loads configuration at startup
- Access database, Redis, or API path settings
- Add new configuration keys with defaults
Code Reference
Source Location
- Repository: MaxKB
- File: apps/maxkb/conf.py
Signature
class Config(dict):
defaults = {
"DB_HOST": "127.0.0.1", "DB_PORT": 5432, "DB_USER": "root",
"DB_PASSWORD": "Password123@postgres", "DB_ENGINE": "dj_db_conn_pool.backends.postgresql",
"DB_MAX_OVERFLOW": 80,
"LOCAL_MODEL_HOST": "127.0.0.1", "LOCAL_MODEL_PORT": 11636,
"LANGUAGE_CODE": "zh-CN", "DEBUG": False,
"REDIS_HOST": "127.0.0.1", "REDIS_PORT": 6379, "REDIS_PASSWORD": "Password123@redis",
"REDIS_DB": 0, "REDIS_MAX_CONNECTIONS": 100,
}
def get_db_setting(self) -> dict:
"""Build Django DATABASES config with connection pool settings."""
def get_cache_setting(self) -> dict:
"""Build Django CACHES config (Redis, with optional Sentinel)."""
def get_admin_path(self) -> str:
"""Return admin API path prefix (default: /admin)."""
def get_chat_path(self) -> str:
"""Return chat API path prefix (default: /chat)."""
class ConfigManager:
config_class = Config
@classmethod
def load_user_config(cls, root_path=None, config_class=None) -> Config:
"""Load config from YAML or env vars based on MAXKB_CONFIG_TYPE."""
def load_from_yml(self) -> bool:
"""Load config from config.yaml/config.yml/config_example.yml."""
def load_from_env(self) -> bool:
"""Load config from MAXKB_* environment variables."""
Import
from maxkb.conf import Config, ConfigManager
from maxkb.const import CONFIG # The global singleton instance
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| root_path | str | No | Directory containing config files (default: PROJECT_DIR) |
| MAXKB_CONFIG_TYPE | env | No | 'ENV' to read env vars, otherwise YAML |
| MAXKB_* | env | Conditional | Environment variables when MAXKB_CONFIG_TYPE=ENV |
| config.yaml | file | Conditional | YAML config file when not ENV mode |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | Config | Dict subclass with all settings + typed accessors |
Usage Examples
Accessing Configuration
from maxkb.const import CONFIG
# Database settings
db_config = CONFIG.get_db_setting()
# {"NAME": "...", "HOST": "...", "PORT": 5432, "ENGINE": "...", "POOL_OPTIONS": {...}}
# Redis cache settings
cache_config = CONFIG.get_cache_setting()
# {"default": {"BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://...", ...}}
# API paths
admin_path = CONFIG.get_admin_path() # "/admin"
chat_path = CONFIG.get_chat_path() # "/chat"
# Direct key access
db_host = CONFIG.get("DB_HOST") # "127.0.0.1"