From d16e31c6757fc59d7548af479b74d33f91ad1f54 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 6 Dec 2022 18:24:25 -0300 Subject: [PATCH] add config file loading --- README.md | 5 +++-- conf.lua | 6 ++++++ config.lua | 36 +++++++++++++++++++++++++++++++++ ctx.lua | 7 ++++++- docker-compose.yaml | 1 + main.lua | 7 ++----- scripts/webfinger_allowlist.lua | 4 ++-- 7 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 conf.lua create mode 100644 config.lua diff --git a/README.md b/README.md index 0721263..da2ec35 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,10 @@ on top of ActivityPub implementations. ## Testing -``` +```sh luarocks-5.1 install --local luaunit luarocks-5.1 install --local lrexlib-PCRE2 eval (luarocks-5.1 path --bin) -lua5.1 test.lua +# luajit works +lua5.1 ./test.lua ``` diff --git a/conf.lua b/conf.lua new file mode 100644 index 0000000..ee662d7 --- /dev/null +++ b/conf.lua @@ -0,0 +1,6 @@ +return { + version = 1, + wantedScripts = { + ['webfinger_allowlist'] = {accounts = {"example@example.com"}} + } +} diff --git a/config.lua b/config.lua new file mode 100644 index 0000000..8759268 --- /dev/null +++ b/config.lua @@ -0,0 +1,36 @@ +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 + local possible_config_path = config_directory .. "/" .. "conf.lua" + local fd = io.open(possible_config_path, "rb") + if fd then + local data = fd:read("*a") + fd:close() + return data + end + end + + return nil +end + +local function loadConfigFile() + local config_file_data = assert(findConfigFile(), 'no config file found, config path: ' .. config_path) + local config_file_function = assert(loadstring(config_file_data)) + return config_file_function() +end + +return loadConfigFile() diff --git a/ctx.lua b/ctx.lua index 98ddcc8..6785f35 100644 --- a/ctx.lua +++ b/ctx.lua @@ -8,10 +8,15 @@ function ctx:setWantedScripts(graph) self._wanted_scripts = graph end +function ctx:loadFromConfig(conf) + ctx:setWantedScripts(conf.wantedScripts) + ctx:loadChain() +end + function ctx:loadChain() self.compiled_chain = {} for module_name, module_config in pairs(self._wanted_scripts) do - local module = require(module_name) + 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}) diff --git a/docker-compose.yaml b/docker-compose.yaml index cee4eee..8cbaf3b 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -8,4 +8,5 @@ services: - 'LUA_PATH=/?.lua;/aproxy/?.lua' volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro + - ./conf.lua:/etc/aproxy/conf.lua:ro - .:/aproxy:ro diff --git a/main.lua b/main.lua index 13ee4f7..c2e8e40 100644 --- a/main.lua +++ b/main.lua @@ -1,4 +1,3 @@ -local CONFIG_PATH = ".;/etc/aproxy" -- function loadConfig() -- -- TODO load config_path @@ -8,11 +7,9 @@ local CONFIG_PATH = ".;/etc/aproxy" -- local config = loadConfig() local ctx = require('ctx') +local conf = require('config') -ctx:setWantedScripts({ - ['scripts.webfinger_allowlist'] = {accounts = {"example@example.com"}} -}) -ctx:loadChain() +ctx:loadFromConfig(conf) return function() ctx:onRequest() diff --git a/scripts/webfinger_allowlist.lua b/scripts/webfinger_allowlist.lua index 4575e7e..b5f5c7e 100644 --- a/scripts/webfinger_allowlist.lua +++ b/scripts/webfinger_allowlist.lua @@ -25,12 +25,12 @@ return { author='luna@l4.pm', title='Webfinger Allowlist', description=[[ - Prevent unecessary DB load by discarding requests to users that we know + Prevent unnecessary DB load by discarding requests to users that we know won't exist. Useful for small instances. ]], - apiVersion=1, + version=1, init=webfingerInit, callbacks = { ['/.well-known/webfinger'] = webfingerCallback