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.setuser then
104 ffluci.sys.process.setuser(track.setuser)
107 if track.setgroup then
108 ffluci.sys.process.setgroup(track.setgroup)
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/"
129 for i,c in ipairs(ffluci.fs.glob(root .. "*/*" .. suff)) do
130 c = "ffluci.controller." .. c:sub(#root+1, #c-#suff):gsub("/", ".", 1)
131 stat, mod = pcall(require, c)
133 if stat and mod and type(mod.index) == "function" then
134 ffluci.util.updfenv(mod.index, ffluci.dispatcher)
140 -- Shortcut for creating a dispatching node
141 function entry(path, target, title, order, add)
149 for k,v in pairs(add) do
156 -- Fetch a dispatching node
160 if arg[1] and type(arg[1]) == "table" then
164 for k,v in ipairs(arg) do
165 if not c.nodes[v] then
166 c.nodes[v] = {nodes={}}
184 function template(name)
185 require("ffluci.template")
186 return function() ffluci.template.render(name) end
190 require("ffluci.cbi")
191 require("ffluci.template")
194 local stat, res = pcall(ffluci.cbi.load, model)
200 local stat, err = pcall(res.parse, res)
206 ffluci.template.render("cbi/header")
208 ffluci.template.render("cbi/footer")