diff options
| author | rebecca <ubq323@ubq323.website> | 2026-09-17 20:54:44 +0100 |
|---|---|---|
| committer | rebecca <ubq323@ubq323.website> | 2026-09-17 20:54:44 +0100 |
| commit | 6c5d5fec129a29c6250b094f46881cfa5aaadcc7 (patch) | |
| tree | b66161fccfa91180e937f0689763f2124a4af97b /web/main.lua | |
| parent | 7f60b7c4e8d99f97a0a24b60bd4c4fd15697a4bb (diff) | |
web: import from m1msq 15b348f
Diffstat (limited to 'web/main.lua')
| -rw-r--r-- | web/main.lua | 58 |
1 files changed, 58 insertions, 0 deletions
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()) |
