所谓配置信息就指 mysql/redis 之类的 ip, port, user, password 之类的信息
是通过 env 保存?还是通过 ini, yml,或者干脆用一个 lua table 保存?
所谓配置信息就指 mysql/redis 之类的 ip, port, user, password 之类的信息
是通过 env 保存?还是通过 ini, yml,或者干脆用一个 lua table 保存?
可以将配置放入文件,然后在每个 module 最开始声明的 local 变量中读取进来,应该都是一个 lua table,例如:
-- config.lua
local _M = {}
fun _M.read()
-- read file
local configs = {
-- ...
}
return configs
end
return _M
-- handler.lua
local configs = requrire("config").read()
fun handler()
local host = configs.host
-- do something
end
NGINX 中使用 env
比较麻烦,需要注意从 NGINX 配置文件中引入,可以借鉴这个帖子:https://forum.openresty.us/d/6362-lua-resty-shell
我比较喜欢用 ini 文件 + resty.ini