global plugin: set 2D vs 3D homepage (+added test)

This commit is contained in:
Leon van Kammen 2026-08-10 13:16:30 +02:00
parent 50c587a70c
commit f61b1ed4ab
12 changed files with 83 additions and 34 deletions

View file

@ -74,10 +74,7 @@ print = function(...) Log(kLogInfo, ... or "''") end -- nice redbean logging fro
-- init URL router
app.url['^/data'] = '/data.lua' -- setup custom file endpoint
app.get('^/$', function(req,res,next)
res.template = 'page/index.html'
next()
end)
app.get('^/$', app.plugin.global.middleware.homepage_2D_or_3D )
app.url['^/admin[/]?$'] = '/page/admin/index.lua'
app.get('^/design[/]?$', util.pagerender('page/design.html'))

View file

@ -5,7 +5,7 @@ return function(app)
local config = { -- default
name = "XRForge",
baseurl="/",
admin_title = "xrforge admin panel",
admin_title = "admin panel",
admin_subtitle = "Manage your XR experiences here",
}

View file

@ -34,6 +34,7 @@ dir.scan = function(path)
if not stat or not unix.S_ISDIR( stat:mode() ) then
return nil,nil, "not a dir"
end -- let redbean take care of serving static file
path = unix.realpath(path)
state,err = assert(unix.opendir(path))
return stat, state, err
end

View file

@ -14,6 +14,7 @@
.col5{ color:#447; }
.col6{ color:#224; }
.col7{ color:#112; }
.col8{ color:#000; }
.page{ width:640px; }

View file

@ -55,7 +55,7 @@ end
app.on('set', function(k,v) -- clear url cache
if k:match("app.opts") then -- after (admin) updates to app.opts.*
foreach( app.cache.url, function(k,v)
app.cache.url = nil
app.cache.url = {}
end)
end
end)

View file

@ -12,21 +12,23 @@ return {
local isdir = urlpath:sub(-1) == "/"
local nslashes = util.countChar('/',urlpath)
local match = function(a,b) return util.stripPath(a) == util.stripPath(b) end
local urlhash = urlpath .. util.dump( req.param )
if urlpath == "/"
or urlpath:match("^"..rootpath)
and not urlpath:gsub(".*/",""):match("%.") then
local redirect = false
if not app.cache.url[ urlpath ] then
if not app.cache.url[ urlhash ] or util.count(req.params) > 0 then
local matchpath = urlpath
if urlpath == '/' then matchpath = rootpath end -- generate janusxr footer for root url
foreach( app.cache.items, function(k,v)
if req.authenticated or app.plugin.moderation.isAllowed(v) then
if v.parent then
trace( "path = " .. util.stripPath(v.path) .. " " ..
"parent = " .. util.stripPath(v.parent.path) .. " " ..
"matchpath = " .. util.stripPath(matchpath)
)
--trace( "path = " .. util.stripPath(v.path) .. " " ..
-- "parent = " .. util.stripPath(v.parent.path) .. " " ..
-- "matchpath = " .. util.stripPath(matchpath)
--)
if match(v.path, matchpath) then -- show parent folder if any
local parent = app.cache.items[ v.parent.id ]
if v.portal then
@ -75,7 +77,7 @@ return {
end
app.pub("dir", nil, {res=res,data=data, urlpath=urlpath, isdir=isdir})
if not redirect then
app.cache.url[ urlpath ] = util.templateFile( res.template, data)
app.cache.url[ urlhash ] = util.templateFile( res.template, data)
end
end
if redirect then
@ -86,7 +88,7 @@ return {
else
res.status(200)
res.header('content-type','text/html')
res.body( app.cache.url[ urlpath ] )
res.body( app.cache.url[ urlhash ] )
next()
end
else

View file

@ -18,7 +18,7 @@
<a href="https://nlnet.nl">NLNET</a><br/>
</td>
<td class="col3">
<a href="/">View from XR Device</a><br/>
<a href="/?view=3D">View from XR Device</a><br/>
<a href="/${opts.plugin_diskscan_dirname}">Content overview</a><br/>
<a href="/about">About</a><br/>
</td>

View file

@ -20,8 +20,7 @@
<div class="margin1L">
<a href="${opts.baseurl}">
<div class="logo max">
<span class="col1">[</span><span class="col2">&lt;&gt;</span><span class="col1">]</span>
<span class="col3">${opts.plugin_global_title}
<span class="col1">[</span><span class="col2">&lt;&gt;</span><span class="col1">]</span>&nbsp;<span class="col3">${opts.plugin_global_title}
{if url:match("admin") then
print('<span class="col1">//<span>')
print('<span class="col0">${opts.admin_title}</span>')

View file

@ -1,17 +0,0 @@
local util = require("util")
if not app.cache['index.html'] then
app.cache['index.html'] = util.memoize(
function()
print("processing!")
local data = { url = util.getURL() .. app.opts.plugin_diskscan_dirname }
local html = util.template( LoadAsset("page/index.html"),data)
return html
end,
60*5 -- re-run every 5 mins (otherwise return cache)
)
end
SetStatus(200)
SetHeader('Content-Type', 'text/html; charset=utf-8')
Write( app.cache['index.html']() )

View file

@ -19,7 +19,7 @@ local plugin = {
key='app.opts.plugin_global_title',
title='Title',
info='Preferably dot-style domain-notation for easy recollection',
default = "your.xr.forge",
default = "xrforge",
values=''
},
{
@ -36,6 +36,13 @@ local plugin = {
default = "map",
values=''
},
{
key='app.opts.plugin_global_homepage',
title='Homepage',
info='Display 3D or 2D experiences on homepage',
default = "3D",
values={ ['3D'] = '3D', ['2D'] = '2D' },
},
{
key='app.opts.plugin_global_topmenu_html',
title='Menu html',
@ -56,6 +63,16 @@ local plugin = {
control='textarea',
values=''
},
},
middleware = {
homepage_2D_or_3D = function(req,res,next)
if (app.opts.plugin_global_homepage == '3D') or (req.param.view == '3D') then
res.template = 'page/index.html'
else
res.template = 'page/dir.html'
end
next()
end
}
}

View file

@ -2,6 +2,19 @@ local lust = require 'test/lust'
local describe, it, expect = lust.describe, lust.it, lust.expect
-- usage: https://github.com/bjornbytes/lust
lust.queueServerRequest = function(method, path, params, host, headers, body, scheme, port)
GetMethod = function() return method end
GetPath = function() return path end
GetParams = function() return params or {} end -- note: { {"key","value"} } format
GetHost = function() return host or 'localhost' end
GetHeaders = function() return headers or {} end
GetScheme = function() return scheme or 'https' end
GetBody = function() return body or '' end
GetPort = function() return port or 80 end
end
require('test/plugin_global')(lust)
os.exit()
require('test/plugin_diskscan')(lust)
require('test/plugin_xrfragment')(lust)

View file

@ -0,0 +1,36 @@
return function(lust)
local describe, it, expect = lust.describe, lust.it, lust.expect
describe('src/plugin/global.lua', function()
describe('set homepage', function() -- Can be nested
it('opts.plugin.global_homepage = "2D"', function()
app.init()
app.get('^/$', app.plugin.global.middleware.homepage_2D_or_3D )
app.use( require('page/dir').middleware ) -- directory browser
app.use( function(req,res,next)
if app.opts.plugin_global_homepage == '2D' and req.param.view == nil then
expect( res.template == 'page/dir.html' ).to.be.truthy()
end
if app.opts.plugin_global_homepage == '2D' and req.param.view == '3D' then
expect( res.template == 'page/index.html' ).to.be.truthy()
end
end)
-- this should render the 2D mode (dir.html)
app.opts.plugin_global_homepage = '2D'
lust.queueServerRequest('GET', '/' )
app.run()
-- this should render the 3D mode (index.html)
lust.queueServerRequest('GET', '/', { {'view','3D'} })
app.run()
end)
end)
end)
end