Skip to content

Commit

Permalink
feat(config): codo gateway 支持注入环境变量
Browse files Browse the repository at this point in the history
  • Loading branch information
Ccheers committed Jun 9, 2024
1 parent efab2b7 commit b84ae5a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ nginx.pid
t/servroot
conf/app-dev.json
#deps

conf/app.json
.travis/
!.travis/ASF-Release.cfg
bin/lj-releng
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ services:
gateway:
restart: unless-stopped
build: .
environment:
- "CODO_GATEWAY.appName=cctest"
- "CODO_GATEWAY.admin.account.admin.info.roles.1=cctest"
ports:
- "8888:8888"
- "11000:11000"
89 changes: 85 additions & 4 deletions gateway/app/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,96 @@ local error = error

local _M = {}

local app_config = {}
local app_config = {
appName = "codo-gateway",
env = "test",
etcd = {
http_host = "http://127.0.0.1:2379",
data_prefix = "/my/gw/",
},
jwt_auth = {
key = "auth_key",
token = "xxxxxx",
},
codo_rbac = {
key = "auth",
token_secret = "xxxxxx",
},
sso2internal = {
sso_token_secret = "xxxxxx",
sso_jwt_key = "sso_token",
internal_token_secret = "xxxxxx",
internal_jwt_key = "auth_key",
},
mfa = {
mfa_secret = "xxxxxx",
mfa_key = "mfa_key"
},
plugins_config = {
["redis-logger"] = {
host = "127.0.0.1",
port = 6379,
auth_pwd = "1234567",
db = 1,
alive_time = 604800,
channel = "gw",
full_log = "no"
}
},
admin = {
jwt_secret = "xxxx",
account = {
admin = {
password = "tainiubile",
info = {
roles = { "admin" },
introduction = "I am a super administrator",
avatar = "https://xxx.com/1.gif",
name = "管理员"
}
}
}
},
tokens = {
["xxx"] = {
desc = "系统默认 api token"
}
}
}

-- 用于递归地检查配置表并使用环境变量更新
local function update_config_with_env(config, parent_key)
for k, v in pairs(config) do
local env_key = parent_key and (parent_key .. "." .. k) or k
if type(v) == "table" then
-- 递归处理嵌套表
config[k] = update_config_with_env(v, env_key)
else
-- 尝试从环境变量中获取新的配置值
local env_value = os.getenv(env_key)
print("env_key======", env_key)
if env_value then
config[k] = env_value
end
end
end
return config
end

function _M.init(config_file)
if not str_utils.start_with(config_file, "/") then
config_file = ngx_config.prefix() .. config_file
end
app_config = json.decode_json_file(config_file)
local json_conf = json.decode_json_file(config_file)
if not json_conf then
error("load config file failed")
return
end
app_config = json_conf
-- 使用环境变量更新配置
app_config = update_config_with_env(app_config, "CODO_GATEWAY")

print("load config success", json.delay_encode(app_config, false))
end

local function get(key)
Expand All @@ -45,7 +128,6 @@ function _M.get_etcd_config()
return get("etcd")
end


function _M.get_jwt_auth()
return get("jwt_auth")
end
Expand All @@ -62,7 +144,6 @@ function _M.get_codo_rbac()
return get("codo_rbac")
end


function _M.get_plugins_config(plugin_name)
local _plugins_config = get("plugins_config")
if _plugins_config then
Expand Down

0 comments on commit b84ae5a

Please sign in to comment.