From fd9adb3e59087fec2a57cba42f672b6e68249808 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Mon, 2 Sep 2024 18:39:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=81=E8=AE=B8=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E6=9B=B4=E6=94=B9config?= =?UTF-8?q?=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- campux/core/app.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/campux/core/app.py b/campux/core/app.py index 2650c99..517fdab 100644 --- a/campux/core/app.py +++ b/campux/core/app.py @@ -2,6 +2,7 @@ import asyncio import threading +import os import requests import nonebot, nonebot.config @@ -42,6 +43,26 @@ def nonebot_thread(): threading.Thread(target=nonebot_thread).start() +def convert_env_var( + env_var: str, + value_type: type +) -> any: + print(env_var) + if value_type == int: + return int(env_var) + elif value_type == str: + return str(env_var) + elif value_type == bool: + if env_var == 'true': + env_var = 'True' + elif env_var == 'false': + env_var = 'False' + return eval(env_var) + elif value_type == list: + return list(eval(env_var)) + elif value_type == dict: + return dict(eval(env_var)) + async def create_app() -> Application: # 元数据 @@ -60,6 +81,20 @@ async def create_app() -> Application: ) await config.load_config() + + # 读取环境变量进行替换, for config + config_data = config.data.copy() + + # 读取环境变量进行替换, for config + for key in config_data: + env_value = os.environ.get(key) + + if env_value is None: + env_value = os.environ.get(key.upper()) + + if env_value is not None: + config.data[key] = convert_env_var(env_value, value_type=type(config.data[key])) + await config.dump_config() # 缓存管理器