summaryrefslogtreecommitdiff
path: root/web/main.lua
diff options
context:
space:
mode:
Diffstat (limited to 'web/main.lua')
-rw-r--r--web/main.lua58
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())