I'm using OpenResty and my app is setup as:
app/
clients/
photoClient.lua
init.lua
auth.lua
Let's say photoClient
has both an unauthenticated and a authenticated endpoint (similar to an avatar endpoint that is accessible without logging in, but there may be private photos that you need to login first)
So in terms of dependencies, I have:
-- auth.lua
local photoClient = require('app.clients.photoClient')
-- this is used to show avatar on the login page
local auth = {}
auth.isAuthenticated = function ()
-- logic to check authentication
end
return auth
and the client is
-- photoClient.lua
local auth = require('app.auth')
local photoClient = {}
photoClient.privateEndpoint = function()
if (!auth.isAuthenticated()) {
ngx.exit(403)
}
...
end
photoClient.getAvator = function() {
-- this is a public method used by auth
}
return photoClient
This is giving me a circular dependency issue. I've seen on other SO post that you can use global variables, i.e. to do photoClient = photoClient or require('app.clients.photoClient')
but I do not want to use global variables and want to keep each module scoped to itself.
How can I do this?
local auth = require('app.auth')
inside the body ofprivateEndpoint
function – Senhauserrequire
your modules at the beginning, global to the scope of the module itself. If I were to haveanotherPrivateMethod
then I'd have to addlocal auth = require('app.auth')
there too. Lua seems different than other languages such as Node, Python in the matter. – Attorneyatlawrequire
to the top of the Lua script. – Uniocular