Implementation:1Panel dev MaxKB Settings Dispatcher
| Knowledge Sources | |
|---|---|
| Domains | Configuration, Deployment |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
Pattern implementation for runtime Django settings selection via the SERVER_NAME environment variable, provided by MaxKB.
Description
MaxKB's settings module uses a dispatcher pattern: the settings/base/__init__.py module reads the `SERVER_NAME` environment variable and imports either `web.py` (full Django app with 17 INSTALLED_APPS, all middleware, DB, cache, templates, i18n) or `model.py` (minimal app with 4 INSTALLED_APPS for the local embedding server). This is set by main.py based on the service being started. A missing or wrong `SERVER_NAME` silently loads the wrong app set, which is a common deployment pitfall.
Usage
Use this pattern when you need to:
- Understand which settings module is loaded for each service
- Debug issues caused by wrong SERVER_NAME
- Add settings for a new service type
Code Reference
Source Location
- Repository: MaxKB
- File: apps/maxkb/settings/base/__init__.py
Signature
# apps/maxkb/settings/base/__init__.py
import os
if os.environ.get('SERVER_NAME', 'web') == 'local_model':
from .model import * # Minimal settings for local_model service
else:
from .web import * # Full Django settings for web service
# main.py sets SERVER_NAME:
if services.__contains__('web'):
os.environ.setdefault('SERVER_NAME', 'web')
elif services.__contains__('local_model'):
os.environ.setdefault('SERVER_NAME', 'local_model')
Import
# Settings are loaded automatically by Django via DJANGO_SETTINGS_MODULE
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "maxkb.settings")
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| SERVER_NAME | env | Yes | 'web' (default) or 'local_model' |
Outputs
| Name | Type | Description |
|---|---|---|
| INSTALLED_APPS | 17 apps (web) or 4 apps (model) | |
| DATABASES | Full DB config (web) or none (model) | |
| CACHES | Redis config (web) or local memory (model) |
Usage Examples
Understanding Settings Selection
# When main.py starts the web service:
os.environ['SERVER_NAME'] = 'web'
# → settings/base/__init__.py imports web.py
# → Full Django app with DB, cache, REST framework, all apps
# When main.py starts the local_model service:
os.environ['SERVER_NAME'] = 'local_model'
# → settings/base/__init__.py imports model.py
# → Minimal app: contenttypes, messages, staticfiles, rest_framework, local_model