xrforge-v2/src/.init.lua

164 lines
5.6 KiB
Lua

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"
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",
bindir = arg[-1]:gsub("xrforge.com",""),
cache = { items = {}, url = {} },
plugin = {},
opts = {},
forkpids = {},
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"},
generate = {file="cmd/generate.lua", info="generate www-folder with XR experiences"},
test = {file="cmd/test.lua", info="run testsuite (selftest)"}
}
}
-- 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.xrfragments = require('plugin/xrfragments')
app.plugin.janusxr = require('plugin/janusxr')
app.plugin.manyfold = require('plugin/manyfold')
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.xrfragments
app.plugin[5] = app.plugin.manyfold
app.runcmd(app) -- run clicmds
print = function(...) Log(kLogInfo, ... or "''") end -- nice redbean logging from now on
-- init URL router
app.url['^/data'] = '/data.lua' -- setup custom file endpoint
app.url['^/$'] = '/page/index.lua'
app.url['^/admin[/]?$'] = '/page/admin/index.lua'
app.get('^/design[/]?$', util.pagerender('page/design.html'))
app.get('^/about[/]?$', util.pagerender('page/about.html'))
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 --
.use( require("middleware/httpauth")() )
.use( function(req,res,next)
local tile = require('page/partial/tile')
local rootpath = "/" .. app.opts.plugin_diskscan_dirname
local urlpath = GetPath()
local isdir = urlpath:sub(-1) == "/"
local nslashes = util.countChar('/',urlpath)
res.items = {}
local match = function(a,b) return util.stripPath(a) == util.stripPath(b) end
if urlpath:match("^"..rootpath) then
if not app.cache.url[ urlpath ] then
foreach( app.cache.items, function(k,v)
if v.parent then
trace( "path = " .. util.stripPath(v.path) .. " " ..
"parent = " .. util.stripPath(v.parent.path) .. " " ..
"urlpath = " .. util.stripPath(urlpath)
)
if match(v.path, urlpath) then -- show parent folder if any
local parent = app.cache.items[ v.parent.id ]
if v.dir then
res.items[ parent.id ] = parent.portal or parent -- portal = symlinked item
print("222222222")
end
if match(parent.path, app.opts.plugin_diskscan_dir) then -- add rootfolder if needed
local root = app.cache.items[ app.opts.plugin_diskscan_dir ]
res.items[ root.id ] = root
end
end
-- add items from dir
if match(v.parent.path, urlpath) then
local parent = app.cache.items[ v.parent.id ]
res.items[ v.id ] = v
--if parent.parent then
-- res.items[ v.id ] = v
--else
-- res.items[ parent.id ] = parent
--end
end
end
end)
local data = util.merge({items=''},{ opts = app.opts })
foreach( res.items, function(k,v)
data.items = data.items .. tile( util.flatten(v,"item") )
end)
app.cache.url[ urlpath ] = util.template( LoadAsset("page/dir.html"), data)
end
res.status(200)
res.header('content-type','text/html; charset=utf-8')
res.body( app.cache.url[ urlpath ] )
next()
else
next()
end
end)
.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
app.on('init', function()
--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
end)
function OnHttpRequest() app.run() end
OnServerStart = function()
app.init() -- this triggers init event
end
OnServerStop = function()
print("todo: kill threads")
end