add initialization callback for filters
This commit is contained in:
parent
eefdecb1e9
commit
a747fe8182
2 changed files with 21 additions and 19 deletions
10
main.lua
10
main.lua
|
@ -18,8 +18,10 @@ local WANTED_SCRIPTS = {
|
|||
local compiled_chain = {}
|
||||
|
||||
for module_name, module_config in pairs(WANTED_SCRIPTS) do
|
||||
module = require(module_name)
|
||||
table.insert(compiled_chain, {module, module_config})
|
||||
local module = require(module_name)
|
||||
local module_state = module.init(module_config)
|
||||
-- TODO is it possible to make module_config readonly?
|
||||
table.insert(compiled_chain, {module, module_config, module_state})
|
||||
end
|
||||
|
||||
local function onRequest()
|
||||
|
@ -28,12 +30,12 @@ local function onRequest()
|
|||
-- find out which modules to call based on their regexes
|
||||
local callbacks_to_call = {}
|
||||
for _, filter in ipairs(compiled_chain) do
|
||||
local module, module_config = filter[1], filter[2]
|
||||
local module, module_config, state = unpack(filter)
|
||||
|
||||
for callback_regex, callback_function in pairs(module.callbacks) do
|
||||
local match, error = ngx.re.match(request_uri, callback_regex)
|
||||
if match then
|
||||
table.insert(callbacks_to_call, {callback_function, module_config})
|
||||
table.insert(callbacks_to_call, {callback_function, module_config, state})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,24 +1,23 @@
|
|||
function webfingerCallback(cfg)
|
||||
function webfingerInit(cfg)
|
||||
local accounts_set = {}
|
||||
for _, account in ipairs(cfg.accounts) do
|
||||
accounts_set["acct:" .. account] = true
|
||||
end
|
||||
return accounts_set
|
||||
end
|
||||
|
||||
function webfingerCallback(cfg, accounts_set)
|
||||
local args, err = ngx.req.get_uri_args()
|
||||
if err == "truncated" then
|
||||
return false, 'uri args too long'
|
||||
return 400, 'uri args too long'
|
||||
end
|
||||
|
||||
local resource = args['resource']
|
||||
if resource ~= nil then
|
||||
-- TODO this is O(n) but we can make it O(1) by doing a funny and making
|
||||
-- cfg.accounts be transformed into keys in a table
|
||||
--
|
||||
-- this would require us to do some setup() callback as well as
|
||||
-- the request() callback
|
||||
for _, account in ipairs(cfg.accounts) do
|
||||
if resource == account then
|
||||
return true
|
||||
end
|
||||
end
|
||||
if accounts_set[resource] then
|
||||
return nil
|
||||
else
|
||||
return 404, "Couldn't find user"
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
return {
|
||||
|
@ -32,6 +31,7 @@ return {
|
|||
Useful for small instances.
|
||||
]],
|
||||
apiVersion=1,
|
||||
init=webfingerInit,
|
||||
callbacks = {
|
||||
['/.well-known/webfinger'] = webfingerCallback
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue