1313import re
1414from importlib import import_module
1515from urllib .parse import urljoin , urlparse
16+
1617import yaml
1718
1819BASE_DIR = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
@@ -75,25 +76,18 @@ class DoesNotExist(Exception):
7576class 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
0 commit comments