Hi all,
Loading modules with the new "init_by_lua" makes a lot of sense for my applications. But additionally I'd like to be able to set configuration options in my module both in a "per-request" way, and also "globally" on server reload, depending on the use case. Something like:
init_by_lua '
mod = require "my.module"
mod.set("param", "value")
';
location / {
content_by_lua '
if something then
mod.set("param", "value2")
end
mod.do_something()
';
}
So the point being that parameters set during the init phase are set directly in the module, allowing you to override defaults (once) for all locations. And parameters that are set during other phases are stored in ngx.ctx, so that each request can have different options based on conditionals.
The only problem is that I can't see how my module can know if it is being loaded during "init_by_lua", or "content_by_lua" etc? I was hoping I could test for ngx.ctx ~= nil, but this throws an error. Is there a way to detect the current phase that I haven't seen?
Also, just to check I'm not missing anything with regards to "init_by_lua"? That is, is it safe to load a module globally and supply it with some default parameters.. and these values would be present in my module after the server reloads, consistent across worker processes?
Cheers,
--
James Hurst
+44 (0) 7979 817 702 | @pintsized
River Rat Records
Politi Arts, 10 Manor Road, London, N16 5SA