xrforge-v2/src/.init.lua

126 lines
4.1 KiB
Lua
Raw Normal View History

2026-07-21 19:19:52 +02:00
package.path = package.path .. ";.lua/?.lua"
package.path = package.path .. ";src/.lua/?.lua"
package.path = package.path .. ";middleware/?.lua"
package.path = package.path .. ";cmd/?.lua"
package.path = package.path .. ";plugin/?.lua"
2026-07-21 19:19:52 +02:00
util = require "util"
json = require "json"
-- special script called by main redbean process at startup
HidePath('/usr/share/zoneinfo/')
HidePath('/usr/share/ssl/')
-- create
app = require("soakbean") {
bin = "./xrforge.com",
2026-07-22 18:36:02 +02:00
bindir = arg[-1]:gsub("xrforge.com",""),
cache = { items = {} },
plugin = {},
2026-07-22 18:36:02 +02:00
opts = {},
2026-07-28 08:50:24 +02:00
forkpids = {},
2026-07-21 19:19:52 +02:00
cmd={
help = {file="cmd/help.lua", info="display all possible flags"},
server = {file="cmd/server.lua", info="starts the webserver"},
createplugin = {file="cmd/createplugin.lua", info="generates skeleton plugin"},
2026-07-23 21:34:09 +02:00
generate = {file="cmd/generate.lua", info="generate www-folder with XR experiences"},
test = {file="cmd/test.lua", info="run testsuite (selftest)"}
2026-07-22 18:36:02 +02:00
}
2026-07-21 19:19:52 +02:00
}
2026-07-22 18:36:02 +02:00
-- api skeleton (implemented via soakbean's app.on(...))
app.set = function(k,v) end
app.init = function() end
-- load config
app.opts = require('config')(app)
-- load system plugins
app.plugin.moderation = require('plugin/moderation')
app.plugin.diskscan = require('plugin/diskscan')
app.plugin.manyfold = require('plugin/manyfold')
app.plugin.janusxr = require('plugin/janusxr')
app.plugin.git = require('plugin/git')
app.plugin.clouddrive = require('plugin/clouddrive')
app.plugin.webhook = require('plugin/webhook')
app.plugin.markdown = require('plugin/markdown')
-- ensure plugin config-defaults
foreach( app.plugin, function(name,plugin)
foreach( plugin.opts, function(k,opt)
local key = opt.key:gsub("app.opts.","")
if not app.opts[ key ] then
app.opts[ key ] = opt.default
end
end)
end)
-- set order preference (optional)
app.plugin[1] = app.plugin.diskscan
app.plugin[2] = app.plugin.moderation
app.plugin[3] = app.plugin.janusxr
app.plugin[4] = app.plugin.manyfold
2026-07-22 18:36:02 +02:00
app.runcmd(app) -- run clicmds
2026-07-23 21:34:09 +02:00
print = function(...) Log(kLogInfo, ... or "''") end -- nice redbean logging from now on
2026-07-22 18:36:02 +02:00
-- init URL router
2026-07-21 19:19:52 +02:00
app.url['^/data'] = '/data.lua' -- setup custom file endpoint
2026-07-23 10:01:34 +02:00
app.url['^/$'] = '/page/index.lua'
app.url['^/admin[/]?$'] = '/page/admin/index.lua'
2026-07-22 18:36:02 +02:00
app.get('^/design[/]?$', util.pagerender('page/design.html'))
2026-07-23 10:01:34 +02:00
app.get('^/about[/]?$', util.pagerender('page/about.html'))
2026-07-22 18:36:02 +02:00
2026-07-21 19:19:52 +02:00
app.post('^/save', function(req,res,next) -- setup inline POST endpoint
-- also .get(), .put(), .delete(), .options()
app.cache = req.body -- middleware auto-decodes json
res.status(200)
res.body({cache=app.cache}) -- middleware auto-encodes json
next()
end)
app --
2026-07-22 18:36:02 +02:00
.use( require("middleware/httpauth")() )
2026-07-28 08:50:24 +02:00
.use( function(req,res,next)
local diskpath = app.opts.plugin_diskscan_dir
local urlpath = GetPath()
print( urlpath .. " => " .. diskpath)
foreach( app.cache.items, function(k,v)
print(v.path)
--local itempath = v.path:gsub("^.","")
--if itempath:sub(0,#diskpath) == diskpath then
-- local _, nslashes = itempath:gsub("/","")
-- local _, diskslashes = diskpath:gsub("/","")
-- if nslashes == diskslashes + 1 then
-- print("slashes = " .. nslashes .. " => " .. diskslashes)
-- table.insert(items,v)
-- end
--end
end)
end)
2026-07-21 19:19:52 +02:00
.use( require("json").middleware() ) -- try plug'n'play json API middleware
.use( app.router( app.url ) ) -- try url router
.use( app.response() ) -- try serve app response (if any)
.use( function(req,next) Route() end) -- fallback default redbean fileserver
2026-07-22 18:36:02 +02:00
app.on('init', function()
2026-07-28 08:50:24 +02:00
--if assert(unix.fork()) == 0 then
-- local ok, headers, body = Fetch('http://127.0.0.1:8080/test')
-- print_r({ok=ok,header=headers,body=body})
--end
2026-07-22 18:36:02 +02:00
end)
function OnHttpRequest() app.run() end
2026-07-22 18:36:02 +02:00
OnServerStart = function()
app.init() -- this triggers init event
end
2026-07-28 08:50:24 +02:00
OnServerStop = function()
print("todo: kill threads")
end