aproxy/main.lua

57 lines
1.6 KiB
Lua
Raw Normal View History

2022-12-06 03:36:15 +00:00
local CONFIG_PATH = ".;/etc/aproxy"
-- function loadConfig()
-- -- TODO load config_path
-- return require("./config.lua")
-- end
--
-- local config = loadConfig()
function log(msg)
ngx.log(ngx.STDERR, tostring(msg))
end
local WANTED_SCRIPTS = {
['scripts.webfinger_allowlist'] = {accounts = {"example@example.com"}}
2022-12-06 03:36:15 +00:00
}
local compiled_chain = {}
for module_name, module_config in pairs(WANTED_SCRIPTS) do
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})
2022-12-06 03:36:15 +00:00
end
local function onRequest()
local request_uri = ngx.var.uri
-- 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, 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, state})
end
end
end
2022-12-06 03:36:15 +00:00
log('AWOOOOGA')
for _,tuple in ipairs(callbacks_to_call) do
local callback_function, config, state = unpack(tuple)
local status_code, body = callback_function(config, state)
if status_code ~= nil then
ngx.status = status_code
2022-12-06 03:36:15 +00:00
ngx.say(body or "request denied")
ngx.exit(status_code)
end
end
2022-12-06 03:36:15 +00:00
end
return onRequest