Chinaunix首页 | 论坛 | 博客
  • 博客访问: 864308
  • 博文数量: 286
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1841
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-09 16:26
文章分类

全部博文(286)

文章存档

2016年(38)

2015年(248)

我的朋友

分类: Web开发

2015-06-26 11:12:43

原文地址:http://blog.csdn.net/hui523hui523hui523/article/details/38943693

代码分析

1,启动:

在浏览器中输入: 会自动跳到cgi-bin/luci

luci\modules\base\htdocs\cgi-bin\luci

  1. #!/usr/bin/lua -- 执行命令的路径
  2. require"luci.cacheloader" -- 导入 cacheloader 包
  3. require"luci.sgi.cgi" -- 导入 sgi.cgi 包
  4. luci.dispatcher.indexcache = "/tmp/luci-indexcache" --cache 缓存路径地址
  5. luci.sgi.cgi.run() -- 执 行 run 方法,此方法位于 luci\modules\base\luasrc\sgi\cgi.lua 中


run方法


  1. luci\modules\base\luasrc\sgi\cgi.lua
  1. function run()
  2. local r = luci.http.Request(
  3. luci.sys.getenv(),
  4. limitsource(io.stdin, tonumber(luci.sys.getenv("CONTENT_LENGTH"))),
  5. ltn12.sink.file(io.stderr)
  6. )
  7. local x = coroutine.create(luci.dispatcher.httpdispatch) //开启协助线程---->调用luci\modules\base\luasrc\dispatcher.lua里的httpdispatch函数
  8. local hcache = ""
  9. local active = true
  10. while coroutine.status(x) ~= "dead" do
  11. local res, id, data1, data2 = coroutine.resume(x, r)
  12. if not res then
  13. print("Status: 500 Internal Server Error")
  14. print("Content-Type: text/plain\n")
  15. print(id)
  16. break;
  17. end
  18. if active then
  19. if id == 1 then
  20. io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
  21. elseif id == 2 then
  22. hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"
  23. elseif id == 3 then
  24. io.write(hcache)
  25. io.write("\r\n")
  26. elseif id == 4 then
  27. io.write(tostring(data1 or ""))
  28. elseif id == 5 then
  29. io.flush()
  30. io.close()
  31. active = false
  32. elseif id == 6 then
  33. data1:copyz(nixio.stdout, data2)
  34. data1:close()
  35. end
  36. end
  37. end
  38. end






2,进入网页


在浏览器中输入: 会自动跳到cgi-bin/luci 登陆默认root 密码是空,默认的几个一级菜单都是在modules\admin-full\luasrc\controller\admin\ 这个目录下,index.lua为执行文件


  1. module("luci.controller.admin.index", package.seeall) //声明一下这个模块, 模块入口为函数
  2. function index()
  3. local root = node() //定义了最外面的节点,也就是最上层的菜单显示
  4. if not root.target then
  5. root.target = alias("admin")
  6. root.index = true
  7. end
  8. local page = node("admin")
  9. page.target = firstchild() //----->luci\modules\base\luasrc\dispatcher.lua--->firstchild()
  10. page.title = _("Administration")
  11. page.order = 10
  12. page.sysauth = "root"
  13. page.sysauth_authenticator = "htmlauth" //---->luci\modules\base\luasrc\dispatcher.lua---->htmlauth() 找到哪个用户
  14. page.ucidata = true
  15. page.index = true
  16. -- Empty services menu to be populated by addons
  17. entry({"admin", "services"}, firstchild(), _("Services"), 40).index = true
  18. entry({"admin", "logout"}, call("action_logout"), _("Logout"), 90)
  19. 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

  1. function authenticator.htmlauth(validator, accs, default)
  2. local user = luci.http.formvalue("username")
  3. local pass = luci.http.formvalue("password")
  4. if user and validator(user, pass) then
  5. return user
  6. end
  7. require("luci.i18n")
  8. require("luci.template")
  9. context.path = {}
  10. luci.template.render("sysauth", {duser=default, fuser=user})
  11. return false
  12. end


4.子菜单项


  1. function _firstchild()
  2. local path = { unpack(context.path) }
  3. local name = table.concat(path, ".") //当前目录下,既:luci\modules\admin-full\luasrc\controller\admin\
  4. local node = context.treecache[name]
  5. local lowest
  6. if node and node.nodes and next(node.nodes) then
  7. local k, v
  8. for k, v in pairs(node.nodes) do
  9. if not lowest or
  10. (v.order or 100) < (node.nodes[lowest].order or 100)
  11. then
  12. lowest = k
  13. end
  14. end
  15. end
  16. assert(lowest ~= nil,
  17. "The requested node contains no childs, unable to redispatch")
  18. path[#path+1] = lowest
  19. dispatch(path)
  20. end
  21. --- Alias the first (lowest order) page automatically
  22. function firstchild()
  23. return { type = "firstchild", target = _firstchild }
  24. end



既:
  1. entry({"admin", "services"}, {"firstchild","network.lua system.lua ......"}, _("Services"), 40).index = true
如上可以看出,登陆后第一级目录如下:luci\modules\admin-full\luasrc\controller\admin\下的xxx.lua文件




增加一个菜单:






阅读(1502) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~