5 The request dispatcher and module dispatcher generators
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
26 module("ffluci.dispatcher", package.seeall)
27 require("ffluci.http")
31 -- Local dispatch database
32 local tree = {nodes={}}
34 -- Global request object
37 -- Active dispatched node
42 function build_url(...)
43 return ffluci.http.dispatcher() .. "/" .. table.concat(arg, "/")
46 -- Sends a 404 error code and renders the "error404" template if available
47 function error404(message)
48 ffluci.http.status(404, "Not Found")
49 message = message or "Not Found"
51 require("ffluci.template")
52 if not pcall(ffluci.template.render, "error404") then
53 ffluci.http.prepare_content("text/plain")
59 -- Sends a 500 error code and renders the "error500" template if available
60 function error500(message)
61 ffluci.http.status(500, "Internal Server Error")
63 require("ffluci.template")
64 if not pcall(ffluci.template.render, "error500", {message=message}) then
65 ffluci.http.prepare_content("text/plain")
71 -- Dispatches a request depending on the PATH_INFO variable
72 function httpdispatch()
73 local pathinfo = ffluci.http.env.PATH_INFO or ""
76 for s in pathinfo:gmatch("/([%w-]+)") do
77 table.insert(request, s)
87 for i, s in ipairs(request) do
93 for k, v in pairs(c) do
100 require("ffluci.i18n").loadc(track.i18n)
103 if track.setgroup then
104 ffluci.sys.process.setgroup(track.setgroup)
107 if track.setuser then
108 ffluci.sys.process.setuser(track.setuser)
112 if c and type(c.target) == "function" then
115 stat, err = pcall(c.target)
125 -- Calls the index function of all available controllers
126 function createindex()
127 local root = ffluci.sys.libpath() .. "/controller/"
130 local controllers = ffluci.util.combine(
131 ffluci.fs.glob(root .. "*" .. suff),
132 ffluci.fs.glob(root .. "*/*" .. suff)
135 for i,c in ipairs(controllers) do
136 c = "ffluci.controller." .. c:sub(#root+1, #c-#suff):gsub("/", ".")
137 stat, mod = pcall(require, c)
139 if stat and mod and type(mod.index) == "function" then
140 ffluci.util.updfenv(mod.index, ffluci.dispatcher)
146 -- Shortcut for creating a dispatching node
147 function entry(path, target, title, order, add)
155 for k,v in pairs(add) do
162 -- Fetch a dispatching node
166 if arg[1] and type(arg[1]) == "table" then
170 for k,v in ipairs(arg) do
171 if not c.nodes[v] then
172 c.nodes[v] = {nodes={}}
190 function template(name)
191 require("ffluci.template")
192 return function() ffluci.template.render(name) end
196 require("ffluci.cbi")
197 require("ffluci.template")
200 local stat, res = pcall(ffluci.cbi.load, model)
206 local stat, err = pcall(res.parse, res)
212 ffluci.template.render("cbi/header")
214 ffluci.template.render("cbi/footer")