From 6c5d5fec129a29c6250b094f46881cfa5aaadcc7 Mon Sep 17 00:00:00 2001 From: rebecca Date: Thu, 17 Sep 2026 20:54:44 +0100 Subject: web: import from m1msq 15b348f --- web/main.lua | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 web/main.lua (limited to 'web/main.lua') diff --git a/web/main.lua b/web/main.lua new file mode 100644 index 0000000..0368063 --- /dev/null +++ b/web/main.lua @@ -0,0 +1,58 @@ +local cqueues = require 'cqueues' +local cq_notify = require 'cqueues.notify' +local posix_dirent = require 'posix.dirent' + +local server = require 'lib.server' +local app = require 'app' +local db = require 'lib.db' + +local pprint = require'pprint' + +-- should happen on code reload too probably +app:run_hooks('pre_start') + +print'NEW CQUEUE' +local cq = cqueues.new() +server.run(cq, app) + +local function reload(modname) + local oldmod = require(modname) + assert(type(oldmod) == "table","reloading only works on modules that return tables") + package.loaded[modname] = nil + local newmod = require(modname) + package.loaded[modname]=oldmod + assert(type(newmod) == "table","new module doesn't seem to be a table, cancelling") + print('','r',modname,oldmod.version,newmod.version) + for k in pairs(oldmod) do oldmod[k] = nil end + for k,v in pairs(newmod) do oldmod[k] = v end +end + +local function reloader() + print('NEW RELOADER') + local path = '.' + local function modname(filename) return filename:match"^([a-z_]+)%.lua$" end + + local notifier = cq_notify.opendir(path) + + local seen = {} + local function add(filename) + if not seen[filename] then seen[filename]=true print('adding',filename) notifier:add(filename) end + end + + for filename in posix_dirent.files(path) do + local m = modname(filename) + if m and m ~= 'main' then add(filename) end + end + + for changes, filename in notifier:changes() do + pprint('changes',changes,filename) + local m = modname(filename) + if m then + print('!!! reloading ['..m..']') + print(pcall(reload, m)) + end + end +end +cq:wrap(reloader) + +assert(cq:loop()) -- cgit v1.2.3