diff --git a/config.lua b/config.lua index 278ee9b..451a3e7 100644 --- a/config.lua +++ b/config.lua @@ -2,19 +2,8 @@ local env_config_path = os.getenv('APROXY_CONFIG_PATH') local DEFAULT_CONFIG_PATH = ".;/etc/aproxy" local config_path = env_config_path or DEFAULT_CONFIG_PATH -function mysplit (inputstr, sep) - if sep == nil then - sep = "%s" - end - local t={} - for str in string.gmatch(inputstr, "([^"..sep.."]+)") do - table.insert(t, str) - end - return t -end - local function findConfigFile() - for _, config_directory in ipairs(mysplit(config_path, ";")) do + for _, config_directory in ipairs(string.split(config_path, ";")) do local possible_config_path = config_directory .. "/" .. "conf.lua" local fd, res = io.open(possible_config_path, "rb") if fd then @@ -37,7 +26,7 @@ end local function fakeTableSchema(value_schema) return setmetatable({ __list = true }, { - __index = function (self, key) + __index = function () return value_schema end }) @@ -45,8 +34,8 @@ end local SPECIAL_KEYS = {__list = true} -local function validateSchema(schema, input, errors) - local errors = errors or {} +local function validateSchema(schema, input, errors_in) + local errors = errors_in or {} if schema.__list then -- generate full schema for lists that are same size as input for k, _ in pairs(input) do schema[k] = schema.__schema_value end @@ -105,12 +94,8 @@ local function validateConfigFile(config_object) return all_schema_errors end -local function writeSchemaErrors(errors, out) - out('sex') -end - -local function loadConfigFile(options) - local options = options or {} +local function loadConfigFile(options_in) + local options = options_in or {} local config_file_data = assert(findConfigFile(), 'no config file found, config path: ' .. config_path) local config_file_function = assert(loadstring(config_file_data)) diff --git a/ctx.lua b/ctx.lua index 78e3239..fb43089 100644 --- a/ctx.lua +++ b/ctx.lua @@ -1,3 +1,5 @@ +local util = require("util") + function log(msg) ngx.log(ngx.STDERR, tostring(msg)) end @@ -13,13 +15,14 @@ function ctx:loadFromConfig(conf) ctx:loadChain() end + function ctx:loadChain() self.compiled_chain = {} for module_name, module_config in pairs(self._wanted_scripts) do local module = require('scripts.' .. module_name) - local module_state = module.init(module_config) - -- TODO is it possible to make module_config readonly? - table.insert(self.compiled_chain, {module, module_config, module_state}) + local module_config_readonly = table.readonly(module_config) + local module_state = module.init(module_config_readonly) + table.insert(self.compiled_chain, {module, module_config_readonly, module_state}) end end @@ -32,7 +35,7 @@ function ctx:onRequest() 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) + local match = ngx.re.match(request_uri, callback_regex) if match then table.insert(callbacks_to_call, {module, callback_function, module_config, state}) end @@ -47,6 +50,7 @@ function ctx:onRequest() ngx.status = status_code ngx.say(body or "request denied") ngx.exit(status_code) + return end end end diff --git a/main.lua b/main.lua index b0cf8ff..0470522 100644 --- a/main.lua +++ b/main.lua @@ -1,11 +1,3 @@ - --- function loadConfig() --- -- TODO load config_path --- return require("./config.lua") --- end --- --- local config = loadConfig() - local ctx = require('ctx') local config = require('config') require('util') diff --git a/scripts/webfinger_allowlist.lua b/scripts/webfinger_allowlist.lua index 41d5c80..7e7355f 100644 --- a/scripts/webfinger_allowlist.lua +++ b/scripts/webfinger_allowlist.lua @@ -1,4 +1,4 @@ -function webfingerInit(cfg) +local function webfingerInit(cfg) local accounts_set = {} for _, account in ipairs(cfg.accounts) do accounts_set["acct:" .. account] = true @@ -6,7 +6,7 @@ function webfingerInit(cfg) return accounts_set end -function webfingerCallback(cfg, accounts_set) +local function webfingerCallback(cfg, accounts_set) local args, err = ngx.req.get_uri_args() if err == "truncated" then return 400, 'uri args too long' diff --git a/test.lua b/test.lua index 968117b..ded2270 100644 --- a/test.lua +++ b/test.lua @@ -64,7 +64,7 @@ function setupTest(module_require_path, input_config) local count = table.pprint(schema_errors) lu.assertIs(count, 0) - state = module.init(input_config) + local state = module.init(input_config) ctx.compiled_chain = { {module, input_config, state} } @@ -74,9 +74,9 @@ end function onRequest() ctx:setWantedScripts() - local ctx = require('ctx') + local context = require('ctx') do - ctx:onRequest() + context:onRequest() end end diff --git a/util.lua b/util.lua index 4db2229..5c0b74f 100644 --- a/util.lua +++ b/util.lua @@ -4,11 +4,11 @@ function table.len(t) return count end -function table.pprint(t, options, ident, total_count) - local ident = ident or 0 - local total_count = total_count or 0 +function table.pprint(t, options_in, ident_in, total_count_in) + local ident = ident_in or 0 + local total_count = total_count_in or 0 - local options = options or {} + local options = options_in or {} local print_function = options.call or print if type(t) == 'table' then local count = 0 @@ -18,7 +18,7 @@ function table.pprint(t, options, ident, total_count) total_count = table.pprint(v, options, ident + 1, total_count) end if count == 0 then - --print('') + print_function('{}') end else print_function(string.rep('\t', ident) .. tostring(t)) @@ -26,3 +26,26 @@ function table.pprint(t, options, ident, total_count) end return total_count end + +function table.readonly(t) + return setmetatable({}, { + __index = t, + __newindex = function () + error("Attempt to modify read-only table") + end, + __metatable = false + }); +end + +function string.split(inputstr, sep) + if sep == nil then + sep = "%s" + end + local t={} + for str in string.gmatch(inputstr, "([^"..sep.."]+)") do + table.insert(t, str) + end + return t +end + +