summaryrefslogtreecommitdiffhomepage
path: root/src/ffluci/dispatcher.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/ffluci/dispatcher.lua')
-rw-r--r--src/ffluci/dispatcher.lua74
1 files changed, 65 insertions, 9 deletions
diff --git a/src/ffluci/dispatcher.lua b/src/ffluci/dispatcher.lua
index d2998aa493..0f05c3d173 100644
--- a/src/ffluci/dispatcher.lua
+++ b/src/ffluci/dispatcher.lua
@@ -95,6 +95,7 @@ function dispatch(req)
return error404()
else
module.request = request
+ module.dispatcher = module.dispatcher or dynamic
setfenv(module.dispatcher, module)
return module.dispatcher(request)
end
@@ -139,30 +140,85 @@ function httpdispatch()
dispatch({category=cat, module=mod, action=act})
end
--- The Simple View Dispatcher directly renders the template
--- which is placed in ffluci/views/"request.module"/"request.action"
-function simpleview(request)
+
+-- Dispatchers --
+
+
+-- The Action Dispatcher searches the module for any function called
+-- action_"request.action" and calls it
+function action(request)
local i18n = require("ffluci.i18n")
- local tmpl = require("ffluci.template")
local disp = require("ffluci.dispatcher")
i18n.loadc(request.module)
- if not pcall(tmpl.render, request.module .. "/" .. request.action) then
+ local action = getfenv()["action_" .. request.action:gsub("-", "_")]
+ if action then
+ action()
+ else
disp.error404()
end
end
--- The Action Dispatcher searches the module for any function called
--- action_"request.action" and calls it
-function action(request)
+-- The CBI dispatcher directly parses and renders the CBI map which is
+-- placed in ffluci/modles/cbi/"request.module"/"request.action"
+function cbi(request)
local i18n = require("ffluci.i18n")
local disp = require("ffluci.dispatcher")
+ local tmpl = require("ffluci.template")
+ local cbi = require("ffluci.cbi")
i18n.loadc(request.module)
+
+ stat, map = pcall(cbi.load, request.module.."/"..request.action)
+ if stat then
+ tmpl.render("header")
+ map:render()
+ tmpl.render("footer")
+ else
+ disp.error404()
+ end
+end
+
+-- The dynamic dispatchers combines the action, simpleview and cbi dispatchers
+-- in one dispatcher. It tries to lookup the request in this order.
+function dynamic(request)
+ local i18n = require("ffluci.i18n")
+ local disp = require("ffluci.dispatcher")
+ local tmpl = require("ffluci.template")
+ local cbi = require("ffluci.cbi")
+
+ i18n.loadc(request.module)
+
local action = getfenv()["action_" .. request.action:gsub("-", "_")]
if action then
action()
- else
+ return
+ end
+
+ if pcall(tmpl.render, request.module .. "/" .. request.action) then
+ return
+ end
+
+ stat, map = pcall(cbi.load, request.module.."/"..request.action)
+ if stat then
+ tmpl.render("header")
+ map:render()
+ tmpl.render("footer")
+ return
+ end
+
+ disp.error404()
+end
+
+-- The Simple View Dispatcher directly renders the template
+-- which is placed in ffluci/views/"request.module"/"request.action"
+function simpleview(request)
+ local i18n = require("ffluci.i18n")
+ local tmpl = require("ffluci.template")
+ local disp = require("ffluci.dispatcher")
+
+ i18n.loadc(request.module)
+ if not pcall(tmpl.render, request.module .. "/" .. request.action) then
disp.error404()
end
end \ No newline at end of file