* Cleaned up and rewrote dispatchers
This commit is contained in:
parent
b2f9325073
commit
2d95ace789
3 changed files with 107 additions and 78 deletions
|
@ -1,5 +1,5 @@
|
||||||
#!/usr/bin/haserl --shell=luac
|
#!/usr/bin/haserl --shell=luac
|
||||||
package.path = "/usr/lib/lua/?.lua;/usr/lib/lua/?/init.lua;" .. package.path
|
package.path = "/usr/lib/lua/?.lua;/usr/lib/lua/?/init.lua;" .. package.path
|
||||||
package.cpath = "/usr/lib/lua/?.so;" .. package.cpath
|
package.cpath = "/usr/lib/lua/?.so;" .. package.cpath
|
||||||
require("ffluci").dispatch()
|
require("ffluci.dispatcher").httpdispatch()
|
||||||
|
|
||||||
|
|
|
@ -88,6 +88,8 @@ require("ffluci.config")
|
||||||
require("ffluci.sys")
|
require("ffluci.sys")
|
||||||
|
|
||||||
|
|
||||||
|
local tree = {}
|
||||||
|
|
||||||
-- Sets privilege for given category
|
-- Sets privilege for given category
|
||||||
function assign_privileges(category)
|
function assign_privileges(category)
|
||||||
local cp = ffluci.config.category_privileges
|
local cp = ffluci.config.category_privileges
|
||||||
|
@ -159,99 +161,130 @@ end
|
||||||
|
|
||||||
-- The Action Dispatcher searches the module for any function called
|
-- The Action Dispatcher searches the module for any function called
|
||||||
-- action_"request.action" and calls it
|
-- action_"request.action" and calls it
|
||||||
function action(request)
|
function action(...)
|
||||||
local i18n = require("ffluci.i18n")
|
|
||||||
local disp = require("ffluci.dispatcher")
|
local disp = require("ffluci.dispatcher")
|
||||||
|
if not disp._action(...) then
|
||||||
i18n.loadc(request.category .. "_" .. request.module)
|
|
||||||
local action = getfenv()["action_" .. request.action:gsub("-", "_")]
|
|
||||||
if action then
|
|
||||||
action()
|
|
||||||
else
|
|
||||||
disp.error404()
|
disp.error404()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- The CBI dispatcher directly parses and renders the CBI map which is
|
-- The CBI dispatcher directly parses and renders the CBI map which is
|
||||||
-- placed in ffluci/modles/cbi/"request.module"/"request.action"
|
-- placed in ffluci/modles/cbi/"request.module"/"request.action"
|
||||||
function cbi(request)
|
function cbi(...)
|
||||||
local i18n = require("ffluci.i18n")
|
|
||||||
local disp = require("ffluci.dispatcher")
|
local disp = require("ffluci.dispatcher")
|
||||||
local tmpl = require("ffluci.template")
|
if not disp._cbi(...) then
|
||||||
local cbi = require("ffluci.cbi")
|
|
||||||
|
|
||||||
local path = request.category.."_"..request.module.."/"..request.action
|
|
||||||
|
|
||||||
i18n.loadc(request.category .. "_" .. request.module)
|
|
||||||
|
|
||||||
local stat, map = pcall(cbi.load, path)
|
|
||||||
if stat and map then
|
|
||||||
local stat, err = pcall(map.parse, map)
|
|
||||||
if not stat then
|
|
||||||
disp.error500(err)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
tmpl.render("cbi/header")
|
|
||||||
map:render()
|
|
||||||
tmpl.render("cbi/footer")
|
|
||||||
elseif not stat then
|
|
||||||
disp.error500(map)
|
|
||||||
else
|
|
||||||
disp.error404()
|
disp.error404()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- The dynamic dispatchers combines the action, simpleview and cbi dispatchers
|
-- The dynamic dispatcher chains the action, submodule, simpleview and CBI dispatcher
|
||||||
-- in one dispatcher. It tries to lookup the request in this order.
|
-- in this particular order. It is the default dispatcher.
|
||||||
function dynamic(request)
|
function dynamic(...)
|
||||||
local i18n = require("ffluci.i18n")
|
|
||||||
local disp = require("ffluci.dispatcher")
|
local disp = require("ffluci.dispatcher")
|
||||||
local tmpl = require("ffluci.template")
|
if not disp._action(...)
|
||||||
local cbi = require("ffluci.cbi")
|
and not disp._submodule(...)
|
||||||
|
and not disp._simpleview(...)
|
||||||
i18n.loadc(request.category .. "_" .. request.module)
|
and not disp._cbi(...) then
|
||||||
|
disp.error404()
|
||||||
local action = getfenv()["action_" .. request.action:gsub("-", "_")]
|
|
||||||
if action then
|
|
||||||
action()
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local path = request.category.."_"..request.module.."/"..request.action
|
|
||||||
if pcall(tmpl.render, path) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local stat, map = pcall(cbi.load, path)
|
|
||||||
if stat and map then
|
|
||||||
local stat, err = pcall(map.parse, map)
|
|
||||||
if not stat then
|
|
||||||
disp.error500(err)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
tmpl.render("cbi/header")
|
|
||||||
map:render()
|
|
||||||
tmpl.render("cbi/footer")
|
|
||||||
return
|
|
||||||
elseif not stat then
|
|
||||||
disp.error500(map)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
disp.error404()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- The Simple View Dispatcher directly renders the template
|
-- The Simple View Dispatcher directly renders the template
|
||||||
-- which is placed in ffluci/views/"request.module"/"request.action"
|
-- which is placed in ffluci/views/"request.module"/"request.action"
|
||||||
function simpleview(request)
|
function simpleview(...)
|
||||||
local i18n = require("ffluci.i18n")
|
|
||||||
local tmpl = require("ffluci.template")
|
|
||||||
local disp = require("ffluci.dispatcher")
|
local disp = require("ffluci.dispatcher")
|
||||||
|
if not disp._simpleview(...) then
|
||||||
|
disp.error404()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- The submodule dispatcher tries to load a submodule of the controller
|
||||||
|
-- and calls its "action"-function
|
||||||
|
function submodule(...)
|
||||||
|
local disp = require("ffluci.dispatcher")
|
||||||
|
if not disp._submodule(...) then
|
||||||
|
disp.error404()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- Internal Dispatcher Functions --
|
||||||
|
|
||||||
|
function _action(request)
|
||||||
|
local action = getfenv()["action_" .. request.action:gsub("-", "_")]
|
||||||
|
local i18n = require("ffluci.i18n")
|
||||||
|
|
||||||
|
if action then
|
||||||
|
i18n.loadc(request.category .. "_" .. request.module)
|
||||||
|
i18n.loadc(request.category .. "_" .. request.module .. "_" .. request.action)
|
||||||
|
action()
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function _cbi(request)
|
||||||
|
local disp = require("ffluci.dispatcher")
|
||||||
|
local tmpl = require("ffluci.template")
|
||||||
|
local cbi = require("ffluci.cbi")
|
||||||
|
local i18n = require("ffluci.i18n")
|
||||||
|
|
||||||
local path = request.category.."_"..request.module.."/"..request.action
|
local path = request.category.."_"..request.module.."/"..request.action
|
||||||
|
|
||||||
i18n.loadc(request.category .. "_" .. request.module)
|
local stat, map = pcall(cbi.load, path)
|
||||||
if not pcall(tmpl.render, path) then
|
if stat and map then
|
||||||
disp.error404()
|
local stat, err = pcall(map.parse, map)
|
||||||
|
if not stat then
|
||||||
|
disp.error500(err)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
i18n.loadc(request.category .. "_" .. request.module)
|
||||||
|
i18n.loadc(request.category .. "_" .. request.module .. "_" .. request.action)
|
||||||
|
tmpl.render("cbi/header")
|
||||||
|
map:render()
|
||||||
|
tmpl.render("cbi/footer")
|
||||||
|
return true
|
||||||
|
elseif not stat then
|
||||||
|
disp.error500(map)
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function _simpleview(request)
|
||||||
|
local i18n = require("ffluci.i18n")
|
||||||
|
local tmpl = require("ffluci.template")
|
||||||
|
|
||||||
|
local path = request.category.."_"..request.module.."/"..request.action
|
||||||
|
|
||||||
|
local stat, t = pcall(tmpl.Template, path)
|
||||||
|
if stat then
|
||||||
|
i18n.loadc(request.category .. "_" .. request.module)
|
||||||
|
i18n.loadc(request.category .. "_" .. request.module .. "_" .. request.action)
|
||||||
|
t:render()
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function _submodule(request)
|
||||||
|
local i18n = require("ffluci.i18n")
|
||||||
|
local m = "ffluci.controller." .. request.category .. "." ..
|
||||||
|
request.module .. "." .. request.action
|
||||||
|
local stat, module = pcall(require, m)
|
||||||
|
|
||||||
|
if stat and module.action then
|
||||||
|
i18n.loadc(request.category .. "_" .. request.module)
|
||||||
|
i18n.loadc(request.category .. "_" .. request.module .. "_" .. request.action)
|
||||||
|
return pcall(module.action)
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
end
|
end
|
|
@ -27,7 +27,3 @@ module("ffluci", package.seeall)
|
||||||
|
|
||||||
__version__ = "0.3"
|
__version__ = "0.3"
|
||||||
__appname__ = "FFLuCI"
|
__appname__ = "FFLuCI"
|
||||||
|
|
||||||
dispatch = require("ffluci.dispatcher").httpdispatch
|
|
||||||
env = ENV
|
|
||||||
form = FORM
|
|
||||||
|
|
Loading…
Reference in a new issue