原文地址:http://blog.csdn.net/hui523hui523hui523/article/details/38943693
代码分析
1,启动:
在浏览器中输入:
会自动跳到cgi-bin/luci
luci\modules\base\htdocs\cgi-bin\luci
-
#!/usr/bin/lua -- 执行命令的路径
-
require"luci.cacheloader" -- 导入 cacheloader 包
-
require"luci.sgi.cgi" -- 导入 sgi.cgi 包
-
luci.dispatcher.indexcache = "/tmp/luci-indexcache" --cache 缓存路径地址
-
luci.sgi.cgi.run() -- 执 行 run 方法,此方法位于 luci\modules\base\luasrc\sgi\cgi.lua 中
#!/usr/bin/lua -- 执行命令的路径
require"luci.cacheloader" -- 导入 cacheloader 包
require"luci.sgi.cgi" -- 导入 sgi.cgi 包
luci.dispatcher.indexcache = "/tmp/luci-indexcache" --cache 缓存路径地址
luci.sgi.cgi.run() -- 执 行 run 方法,此方法位于 luci\modules\base\luasrc\sgi\cgi.lua 中
run方法
-
luci\modules\base\luasrc\sgi\cgi.lua
luci\modules\base\luasrc\sgi\cgi.lua
-
function run()
-
local r = luci.http.Request(
-
luci.sys.getenv(),
-
limitsource(io.stdin, tonumber(luci.sys.getenv("CONTENT_LENGTH"))),
-
ltn12.sink.file(io.stderr)
-
)
-
-
local x = coroutine.create(luci.dispatcher.httpdispatch)
-
local hcache = ""
-
local active = true
-
-
while coroutine.status(x) ~= "dead" do
-
local res, id, data1, data2 = coroutine.resume(x, r)
-
-
if not res then
-
print("Status: 500 Internal Server Error")
-
print("Content-Type: text/plain\n")
-
print(id)
-
break;
-
end
-
-
if active then
-
if id == 1 then
-
io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
-
elseif id == 2 then
-
hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"
-
elseif id == 3 then
-
io.write(hcache)
-
io.write("\r\n")
-
elseif id == 4 then
-
io.write(tostring(data1 or ""))
-
elseif id == 5 then
-
io.flush()
-
io.close()
-
active = false
-
elseif id == 6 then
-
data1:copyz(nixio.stdout, data2)
-
data1:close()
-
end
-
end
-
end
-
end
function run()
local r = luci.http.Request(
luci.sys.getenv(),
limitsource(io.stdin, tonumber(luci.sys.getenv("CONTENT_LENGTH"))),
ltn12.sink.file(io.stderr)
)
local x = coroutine.create(luci.dispatcher.httpdispatch) //开启协助线程---->调用luci\modules\base\luasrc\dispatcher.lua里的httpdispatch函数
local hcache = ""
local active = true
while coroutine.status(x) ~= "dead" do
local res, id, data1, data2 = coroutine.resume(x, r)
if not res then
print("Status: 500 Internal Server Error")
print("Content-Type: text/plain\n")
print(id)
break;
end
if active then
if id == 1 then
io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
elseif id == 2 then
hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"
elseif id == 3 then
io.write(hcache)
io.write("\r\n")
elseif id == 4 then
io.write(tostring(data1 or ""))
elseif id == 5 then
io.flush()
io.close()
active = false
elseif id == 6 then
data1:copyz(nixio.stdout, data2)
data1:close()
end
end
end
end
2,进入网页
在浏览器中输入:
会自动跳到cgi-bin/luci 登陆默认root 密码是空,默认的几个一级菜单都是在modules\admin-full\luasrc\controller\admin\
这个目录下,index.lua为执行文件
-
module("luci.controller.admin.index", package.seeall)
-
-
function index()
-
local root = node()
-
if not root.target then
-
root.target = alias("admin")
-
root.index = true
-
end
-
-
local page = node("admin")
-
page.target = firstchild()
-
page.title = _("Administration")
-
page.order = 10
-
page.sysauth = "root"
-
page.sysauth_authenticator = "htmlauth"
-
page.ucidata = true
-
page.index = true
-
-
-- Empty services menu to be populated by addons
-
entry({"admin", "services"}, firstchild(), _("Services"), 40).index = true
-
-
entry({"admin", "logout"}, call("action_logout"), _("Logout"), 90)
-
end
module("luci.controller.admin.index", package.seeall) //声明一下这个模块, 模块入口为函数
function index()
local root = node() //定义了最外面的节点,也就是最上层的菜单显示
if not root.target then
root.target = alias("admin")
root.index = true
end
local page = node("admin")
page.target = firstchild() //----->luci\modules\base\luasrc\dispatcher.lua--->firstchild()
page.title = _("Administration")
page.order = 10
page.sysauth = "root"
page.sysauth_authenticator = "htmlauth" //---->luci\modules\base\luasrc\dispatcher.lua---->htmlauth() 找到哪个用户
page.ucidata = true
page.index = true
-- Empty services menu to be populated by addons
entry({"admin", "services"}, firstchild(), _("Services"), 40).index = true
entry({"admin", "logout"}, call("action_logout"), _("Logout"), 90)
end
entry 定义了菜单下的一个子菜单。
entry(路径, 调用目标, _("显示名称"), 显示顺序)
entry(path, target, title=nil, order=nil)
第1项为菜单入口;
第2项为菜单对应的页面,可以是lua的源代码文件,也可以是html页面,甚至可以是以上两种页面的组合。(alias是指向别的entry的别名,from调用的某一个view,cbi调用某一个model,call直接调用函数)
第3项是菜单的文本,直接添加string不会国际化,_("string"),就国际化了
第4项是同级菜单下,此菜单项的位置,从大到小。
3,登陆
目录:luci\modules\base\luasrc\dispatcher.lua
-
function authenticator.htmlauth(validator, accs, default)
-
local user = luci.http.formvalue("username")
-
local pass = luci.http.formvalue("password")
-
-
if user and validator(user, pass) then
-
return user
-
end
-
-
require("luci.i18n")
-
require("luci.template")
-
context.path = {}
-
luci.template.render("sysauth", {duser=default, fuser=user})
-
return false
-
-
end
function authenticator.htmlauth(validator, accs, default)
local user = luci.http.formvalue("username")
local pass = luci.http.formvalue("password")
if user and validator(user, pass) then
return user
end
require("luci.i18n")
require("luci.template")
context.path = {}
luci.template.render("sysauth", {duser=default, fuser=user})
return false
end
4.子菜单项
-
function _firstchild()
-
local path = { unpack(context.path) }
-
local name = table.concat(path, ".")
-
local node = context.treecache[name]
-
-
local lowest
-
if node and node.nodes and next(node.nodes) then
-
local k, v
-
for k, v in pairs(node.nodes) do
-
if not lowest or
-
(v.order or 100) < (node.nodes[lowest].order or 100)
-
then
-
lowest = k
-
end
-
end
-
end
-
-
assert(lowest ~= nil,
-
"The requested node contains no childs, unable to redispatch")
-
-
path[#path+1] = lowest
-
dispatch(path)
-
end
-
-
--- Alias the first (lowest order) page automatically
-
function firstchild()
-
return { type = "firstchild", target = _firstchild }
-
end
function _firstchild()
local path = { unpack(context.path) }
local name = table.concat(path, ".") //当前目录下,既:luci\modules\admin-full\luasrc\controller\admin\
local node = context.treecache[name]
local lowest
if node and node.nodes and next(node.nodes) then
local k, v
for k, v in pairs(node.nodes) do
if not lowest or
(v.order or 100) < (node.nodes[lowest].order or 100)
then
lowest = k
end
end
end
assert(lowest ~= nil,
"The requested node contains no childs, unable to redispatch")
path[#path+1] = lowest
dispatch(path)
end
--- Alias the first (lowest order) page automatically
function firstchild()
return { type = "firstchild", target = _firstchild }
end
既:
-
entry({"admin", "services"}, {"firstchild","network.lua system.lua ......"}, _("Services"), 40).index = true
entry({"admin", "services"}, {"firstchild","network.lua system.lua ......"}, _("Services"), 40).index = true
如上可以看出,登陆后第一级目录如下:luci\modules\admin-full\luasrc\controller\admin\下的xxx.lua文件
增加一个菜单:
阅读(1552) | 评论(0) | 转发(0) |