diff --git a/.gitignore b/.gitignore index f637c51..c9d7551 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ nginx.pid t/servroot conf/app-dev.json #deps - +conf/app.json .travis/ !.travis/ASF-Release.cfg bin/lj-releng diff --git a/docker-compose.yml b/docker-compose.yml index e605e08..4af8a47 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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" diff --git a/gateway/app/config.lua b/gateway/app/config.lua index 92b0054..730d70a 100644 --- a/gateway/app/config.lua +++ b/gateway/app/config.lua @@ -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) @@ -45,7 +128,6 @@ function _M.get_etcd_config() return get("etcd") end - function _M.get_jwt_auth() return get("jwt_auth") end @@ -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