config_util

config_util 配置热加载

1 获取 py 格式配置文件

配置文件示例(butterfly/conf/servicer/demo_servicer.py)

host = "127.0.0.1"
port = 8080
name = "ffff"

bj = {
    "host": "127.0.0.2",
    "port": 8081,
}

from xlib.util import config_util

ip = "0.0.0.0"
reqid = "DEV_CESHI"
wsgienv = {"PATH_INFO": "/echo"}
req = httpgateway.Request(reqid, wsgienv, ip)

# "127.0.0.1"
print(config_util.get_config(req, "demo_servicer", "host"))

req.idc = "bj"
# "127.0.0.2"
print(config_util.get_config(req, "demo_servicer", "host"))
print(config_util.get_config(req, "demo_servicer", "name"))
print(config_util.get_config(None, "demo_servicer", "name"))

2 获取 toml 格式配置文件

配置文件示例(butterfly/conf/servicer/demo_servicer.toml)

host = "127.0.0.1"
name = "ffff"
port = 8080

[bj]
host = "127.0.0.2"
port = 8081

3 获取特定目录下配置

from xlib.util import config_util

# 默认读取的 <conf.config.BASE_DIR>/conf/servicer,可指定 config_path,支持绝对路径和项目的相对路径
host = config_util.get_config(req, "demo_servicer", "host", config_dir_path="conf/servicer")
rule_list = config_util.get_config(req, config_file, "rule_list", "conf/component")

如配置不存在,则返回 None

Last updated