add config file loading

This commit is contained in:
Luna 2022-12-06 18:24:25 -03:00
parent c15abe3c05
commit d16e31c675
7 changed files with 56 additions and 10 deletions

View File

@ -13,9 +13,10 @@ on top of ActivityPub implementations.
## Testing ## Testing
``` ```sh
luarocks-5.1 install --local luaunit luarocks-5.1 install --local luaunit
luarocks-5.1 install --local lrexlib-PCRE2 luarocks-5.1 install --local lrexlib-PCRE2
eval (luarocks-5.1 path --bin) eval (luarocks-5.1 path --bin)
lua5.1 test.lua # luajit works
lua5.1 ./test.lua
``` ```

6
conf.lua Normal file
View File

@ -0,0 +1,6 @@
return {
version = 1,
wantedScripts = {
['webfinger_allowlist'] = {accounts = {"example@example.com"}}
}
}

36
config.lua Normal file
View File

@ -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()

View File

@ -8,10 +8,15 @@ function ctx:setWantedScripts(graph)
self._wanted_scripts = graph self._wanted_scripts = graph
end end
function ctx:loadFromConfig(conf)
ctx:setWantedScripts(conf.wantedScripts)
ctx:loadChain()
end
function ctx:loadChain() function ctx:loadChain()
self.compiled_chain = {} self.compiled_chain = {}
for module_name, module_config in pairs(self._wanted_scripts) do 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) local module_state = module.init(module_config)
-- TODO is it possible to make module_config readonly? -- TODO is it possible to make module_config readonly?
table.insert(self.compiled_chain, {module, module_config, module_state}) table.insert(self.compiled_chain, {module, module_config, module_state})

View File

@ -8,4 +8,5 @@ services:
- 'LUA_PATH=/?.lua;/aproxy/?.lua' - 'LUA_PATH=/?.lua;/aproxy/?.lua'
volumes: volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
- ./conf.lua:/etc/aproxy/conf.lua:ro
- .:/aproxy:ro - .:/aproxy:ro

View File

@ -1,4 +1,3 @@
local CONFIG_PATH = ".;/etc/aproxy"
-- function loadConfig() -- function loadConfig()
-- -- TODO load config_path -- -- TODO load config_path
@ -8,11 +7,9 @@ local CONFIG_PATH = ".;/etc/aproxy"
-- local config = loadConfig() -- local config = loadConfig()
local ctx = require('ctx') local ctx = require('ctx')
local conf = require('config')
ctx:setWantedScripts({ ctx:loadFromConfig(conf)
['scripts.webfinger_allowlist'] = {accounts = {"example@example.com"}}
})
ctx:loadChain()
return function() return function()
ctx:onRequest() ctx:onRequest()

View File

@ -25,12 +25,12 @@ return {
author='luna@l4.pm', author='luna@l4.pm',
title='Webfinger Allowlist', title='Webfinger Allowlist',
description=[[ 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. won't exist.
Useful for small instances. Useful for small instances.
]], ]],
apiVersion=1, version=1,
init=webfingerInit, init=webfingerInit,
callbacks = { callbacks = {
['/.well-known/webfinger'] = webfingerCallback ['/.well-known/webfinger'] = webfingerCallback