Skip to content
Snippets Groups Projects
Commit 3725f7bc authored by Matthias Schiffer's avatar Matthias Schiffer
Browse files

Merge remote-tracking branch 'origin/announce.d'

parents 69d18cc9 9dffb128
No related branches found
No related tags found
No related merge requests found
...@@ -10,24 +10,42 @@ local function collect_entry(entry) ...@@ -10,24 +10,42 @@ local function collect_entry(entry)
if fs.stat(entry, 'type') == 'dir' then if fs.stat(entry, 'type') == 'dir' then
return collect_dir(entry) return collect_dir(entry)
else else
return setfenv(loadfile(entry), _M)() return loadfile(entry)
end end
end end
function collect_dir(dir) function collect_dir(dir)
local ret = { [{}] = true } local fns = {}
for entry in fs.dir(dir) do for entry in fs.dir(dir) do
if entry:sub(1, 1) ~= '.' then if entry:sub(1, 1) ~= '.' then
local ok, val = pcall(collect_entry, dir .. '/' .. entry) collectgarbage()
local fn, err = collect_entry(dir .. '/' .. entry)
if fn then
fns[entry] = fn
else
io.stderr:write(err, '\n')
end
end
end
return function ()
local ret = { [{}] = true }
for k, v in pairs(fns) do
collectgarbage()
local ok, val = pcall(setfenv(v, _M))
if ok then if ok then
ret[entry] = val ret[k] = val
else else
io.stderr:write(val, '\n') io.stderr:write(val, '\n')
end end
end end
end
return ret collectgarbage()
end
return ret
end
end
local announce = require 'gluon.announce' local announce = require 'gluon.announce'
local deflate = require 'deflate' local deflate = require 'deflate'
local json = require 'luci.jsonc' local json = require 'luci.jsonc'
local nixio = require 'nixio'
local fs = require 'nixio.fs'
local memoize = {}
local function collect(type) nixio.chdir('/lib/gluon/announce/')
return announce.collect_dir('/lib/gluon/announce/' .. type .. '.d')
for dir in fs.glob('*.d') do
local name = dir:sub(1, -3)
memoize[name] = announce.collect_dir(dir)
end end
local function collect(type)
return memoize[type] and memoize[type]()
end
module('gluon.announced', package.seeall) module('gluon.announced', package.seeall)
function handle_request(query) function handle_request(query)
if query:match('^nodeinfo$') then collectgarbage()
return json.stringify(collect('nodeinfo'))
end
local m = query:match('^GET ([a-z ]+)$') local m = query:match('^GET ([a-z ]+)$')
local ret
if m then if m then
local data = {} local data = {}
...@@ -27,7 +35,16 @@ function handle_request(query) ...@@ -27,7 +35,16 @@ function handle_request(query)
end end
if next(data) then if next(data) then
return deflate.compress(json.stringify(data)) ret = deflate.compress(json.stringify(data))
end
elseif query:match('^[a-z]+$') then
local ok, data = pcall(collect, query)
if ok then
ret = json.stringify(data)
end end
end end
collectgarbage()
return ret
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment