Skip to content

Commit 2ec9f41

Browse files
authored
feat: 支持通过环境变量修改配置
1 parent 916e5a9 commit 2ec9f41

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-35
lines changed

apps/smartdoc/conf.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import re
1414
from importlib import import_module
1515
from urllib.parse import urljoin, urlparse
16+
1617
import yaml
1718

1819
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -75,25 +76,18 @@ class DoesNotExist(Exception):
7576
class Config(dict):
7677
defaults = {
7778
# 数据库相关配置
78-
"DB_HOST": "",
79-
"DB_PORT": "",
80-
"DB_USER": "",
81-
"DB_PASSWORD": "",
79+
"DB_HOST": "127.0.0.1",
80+
"DB_PORT": 5432,
81+
"DB_USER": "root",
82+
"DB_PASSWORD": "Password123@postgres",
8283
"DB_ENGINE": "django.db.backends.postgresql_psycopg2",
83-
# 邮件相关配置
84-
"EMAIL_ADDRESS": "",
85-
"EMAIL_USE_TLS": False,
86-
"EMAIL_USE_SSL": True,
87-
"EMAIL_HOST": "",
88-
"EMAIL_PORT": 465,
89-
"EMAIL_HOST_USER": "",
90-
"EMAIL_HOST_PASSWORD": "",
9184
# 向量模型
9285
"EMBEDDING_MODEL_NAME": "shibing624/text2vec-base-chinese",
9386
"EMBEDDING_DEVICE": "cpu",
9487
"EMBEDDING_MODEL_PATH": os.path.join(PROJECT_DIR, 'models'),
9588
# 向量库配置
96-
"VECTOR_STORE_NAME": 'pg_vector'
89+
"VECTOR_STORE_NAME": 'pg_vector',
90+
"DEBUG": False
9791

9892
}
9993

@@ -180,8 +174,36 @@ def load_from_yml(self):
180174
loaded = self.from_yaml(i)
181175
if loaded:
182176
return True
177+
msg = f"""
183178
184-
return False
179+
Error: No config file found.
180+
181+
You can run `cp config_example.yml {self.root_path}/config.yml`, and edit it.
182+
183+
"""
184+
raise ImportError(msg)
185+
186+
def load_from_env(self):
187+
keys = os.environ.keys()
188+
config = {key.replace('MAXKB_', ''): os.environ.get(key) for key in keys if key.startswith('MAXKB_')}
189+
if len(config.keys()) <= 1:
190+
msg = f"""
191+
192+
Error: No config env found.
193+
194+
Please set environment variables
195+
MAXKB_CONFIG_TYPE: 配置文件读取方式 FILE: 使用配置文件配置 ENV: 使用ENV配置
196+
MAXKB_DB_NAME: 数据库名称
197+
MAXKB_DB_HOST: 数据库主机
198+
MAXKB_DB_PORT: 数据库端口
199+
MAXKB_DB_USER: 数据库用户名
200+
MAXKB_DB_PASSWORD: 数据库密码
201+
MAXKB_EMBEDDING_MODEL_PATH: 向量模型目录
202+
MAXKB_EMBEDDING_MODEL_NAME: 向量模型名称
203+
"""
204+
raise ImportError(msg)
205+
self.from_mapping(config)
206+
return True
185207

186208
@classmethod
187209
def load_user_config(cls, root_path=None, config_class=None):
@@ -190,15 +212,10 @@ def load_user_config(cls, root_path=None, config_class=None):
190212
if not root_path:
191213
root_path = PROJECT_DIR
192214
manager = cls(root_path=root_path)
193-
if manager.load_from_yml():
194-
config = manager.config
215+
config_type = os.environ.get('MAXKB_CONFIG_TYPE')
216+
if config_type is None or config_type != 'ENV':
217+
manager.load_from_yml()
195218
else:
196-
msg = f"""
197-
198-
Error: No config file found.
199-
200-
You can run `cp config_example.yml {root_path}/config.yml`, and edit it.
201-
202-
"""
203-
raise ImportError(msg)
219+
manager.load_from_env()
220+
config = manager.config
204221
return config

config_example.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
# 邮箱配置
2-
EMAIL_ADDRESS:
3-
EMAIL_USE_TLS: False
4-
EMAIL_USE_SSL: True
5-
EMAIL_HOST: smtp.qq.com
6-
EMAIL_PORT: 465
7-
EMAIL_HOST_USER:
8-
EMAIL_HOST_PASSWORD:
9-
101
# 数据库链接信息
112
DB_NAME: maxkb
123
DB_HOST: localhost

installer/Dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ RUN apt-get update && \
1717

1818
COPY . /opt/maxkb/app
1919
RUN mkdir -p /opt/maxkb/app /opt/maxkb/model /opt/maxkb/conf && \
20-
cp -f /opt/maxkb/app/installer/config.yaml /opt/maxkb/conf && \
2120
rm -rf /opt/maxkb/app/ui
2221
COPY --from=web-build ui /opt/maxkb/app/ui
2322
WORKDIR /opt/maxkb/app
@@ -33,7 +32,16 @@ ARG DOCKER_IMAGE_TAG=dev \
3332
BUILD_AT \
3433
GITHUB_COMMIT
3534

36-
ENV MAXKB_VERSION ${DOCKER_IMAGE_TAG} (build at ${BUILD_AT}, commit: ${GITHUB_COMMIT})
35+
ENV MAXKB_VERSION="${DOCKER_IMAGE_TAG} (build at ${BUILD_AT}, commit: ${GITHUB_COMMIT})" \
36+
MAXKB_CONFIG_TYPE=ENV \
37+
MAXKB_DB_NAME=maxkb \
38+
MAXKB_DB_HOST=127.0.0.1 \
39+
MAXKB_DB_PORT=5432 \
40+
MAXKB_DB_USER=root \
41+
MAXKB_DB_PASSWORD=Password123@postgres \
42+
MAXKB_EMBEDDING_MODEL_NAME=/opt/maxkb/model/embedding/shibing624_text2vec-base-chinese \
43+
MAXKB_EMBEDDING_MODEL_PATH=/opt/maxkb/model/embedding
44+
3745
WORKDIR /opt/maxkb/app
3846
COPY --from=stage-build /opt/maxkb /opt/maxkb
3947
COPY --from=stage-build /opt/py3 /opt/py3

0 commit comments

Comments
 (0)