增加启动参数,方便docker容器读取外部配置文件

This commit is contained in:
RegimenArsenic
2023-04-01 13:57:47 +08:00
parent c4873f2669
commit 9aa331a3d3
2 changed files with 11 additions and 5 deletions

13
app.py
View File

@@ -1,5 +1,6 @@
# encoding:utf-8
import argparse
import config
from channel import channel_factory
from common import log, const
@@ -9,7 +10,7 @@ from multiprocessing import Pool
# 启动通道
def start_process(channel_type):
# 若为多进程启动,子进程无法直接访问主进程的内存空间,重新创建config类
config.load_config()
config.load_config(args.config)
model_type = config.conf().get("model").get("type")
log.info("[INIT] Start up: {} on {}", model_type, channel_type)
@@ -19,10 +20,10 @@ def start_process(channel_type):
# startup channel
channel.startup()
if __name__ == '__main__':
def main():
try:
# load config
config.load_config()
config.load_config(args.config)
model_type = config.conf().get("model").get("type")
channel_type = config.conf().get("channel").get("type")
@@ -60,3 +61,9 @@ if __name__ == '__main__':
except Exception as e:
log.error("App startup failed!")
log.exception(e)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--config", help="config.json path(e.g: ./config.json or /usr/local/bot-on-anything/config.json)",type=str,default="./config.json")
args = parser.parse_args()
main()

View File

@@ -6,9 +6,8 @@ import os
config = {}
def load_config():
def load_config(config_path = "./config.json"):
global config
config_path = "config.json"
if not os.path.exists(config_path):
raise Exception('配置文件不存在请根据config-template.json模板创建config.json文件')