转载和积累系列 - luci框架-LUA的一个web框架使用

http://blog.csdn.net/initphp/article/details/17527639

 

LUCI 这个在百度上搜索除了一篇我的百度文库 luci 的介绍文章之外,前三页都是些不知所云的名词(足见百度在专业领域的搜索之烂),我却在大学毕业的大半年的大部分时间里与它纠结,由于开始的发懵到后来逐渐感觉到这家伙还很好玩的,现在就把我对 luci 的浅显认识介绍给大家。

官网: http://luci.subsignal.org/

  有关luci 的各个方面,你几乎都可以从这里获得,当然,只是浅显的获得, luci 的文档写的还算比较全,但是写的稍显简略,开始看的时候会有一点不知所措。

UCI  熟悉 openwrt 的人都会有所了解,就是 Uni fi ed Con fi guration Interface 的简称,而 luci 这个 openwrt上的默认 web 系统,是一个独立的由严谨的德国人开发的 web 框架,是 Lua  Con fi guration Interface 的简称,如果在您的应用里, luci 是对 openwrt 的服务,我们就有必要做一下 uci 的简介,我这里就不说了,见链接:

http://www.google.com.hk/url?sa=t&source=web&cd=5&ved=0CEMQFjAE&url=http://nbd.name/openwrt-fosdem-09.pdf&ei=h52iTcXvOcrMcJ-xxOwD&usg=AFQjCNGFhumCIgS5tK_mDJ2dDFU4qsskfQ

 

有的时候,我们开发的luci 是在自己的 Linux PC 上开发,在普通的 linux 上,一般是没有 uci 命令的,为了开发方便,可以手动编译一下,方法见链接:

https://forum.openwrt.org/viewtopic.php?id=15243

OK ,之前罗里罗嗦的说了很多,现在就进入正题,进入正题的前提是你已经 make install 正确的安装了 lua  ,luci ,以及编译好链接了相关的 so (如果你需要,比如 uci.so nixio.so ),以及 make install 正确 web server,(我用的 web server 是 thttpd ,也编译过 mongoose , lighttpd ,在这三个之中, lighttpd 是功能最完善的, mongoose 是最小巧的)。

进入正题:

一:luci 的启动

  在web server 中的 cgi-bin 目录下,运行 luci 文件(权限一般是 755 ), luci 的代码如下:

 

[html] view plain copy print ?
  1. #!/usr/bin/lua      --cgi的执行命令的路径  
  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/sgi/cgi.lua中  

 

 run方法的主要任务就是在安全的环境中打开开始页面(登录页面),在 run 中,最主要的功能还是在dispatch.lua 中完成。

 运行luci 之后,就会出现登录界面:

 

[html] view plain copy print ?
  1.  -bash-4.0# pwd    
  2. /var/www/cgi-bin    
  3. -bash-4.0# ./luci    
  4.   Status: 200 OK        
  5.   Content-Type: text/html;     
  6.   charset=utf-8         
  7.   Cache-Control: no-cache       
  8.   Expires: 0    
  9. "http://www.w3.org/TR/html4/strict.dtd">        
  10.  <html class=" ext-strict">    
  11.          
  12.  html>    

 

二:LUCI 的 MVC

1:用户管理:

      在luci 的官方网站说明了 luci 是一个 MVC 架构的框架,这个 MVC 做的可扩展性很好,可以完全的统一的写自己的 html 网页,而且他对 shell 的支持相当的到位,(因为 luci 是 lua 写的, lua 是 C 的儿子嘛,       与 shell 是兄弟)。在登录界面用户名的选择很重要,      luci 是一个单用户框架,公用的模块放置在 */luci/controller/ 下面,各个用户的模块放置在 */luci/controller/ 下面对应的文件夹里面,比如              admin 登录,最终的页面只显示 /luci/controller/admin 下面的菜单。这样既有效的管理了不同管理员的权限。

2: controller 文件夹下的 lua 文件说明:(以 mini 用户为例)

     在mini 目录下面,会有一个 index.lua 文件,简略的代码如下:

 

[html] view plain copy print ?
  1.  module("luci.controller.mini.index", package.seeall)    
  2. 17      
  3. 18  function index()    
  4. 19      luci.i18n.loadc("admin-core")    
  5. 20      local i18n = luci.i18n.translate    
  6. 21      
  7. 22      local root = node()    
  8. 23      if not root.lock then    
  9. 24          root.target = alias("mini")    
  10. 25          root.index = true    
  11. 26      end    
  12. 27         
  13. 28      entry({"about"}, template("about")).i18n = "admin-core"    
  14. 29         
  15. 30      local page   = entry({"mini"}, alias("mini", "index"), i18n("essentials", "Essentials"), 10)    
  16. 31      page.i18n    = "admin-core"    
  17. 32      page.sysauth = "root"    
  18. 33      page.sysauth_authenticator = "htmlauth"    
  19. 34      page.index = true    
  20. 35         
  21. 36      entry({"mini", "index"}, alias("mini", "index", "index"), i18n("overview"), 10).index = true    
  22. 37      entry({"mini", "index", "index"}, form("mini/index"), i18n("general"), 1).ignoreindex = true    
  23. 38      entry({"mini", "index", "luci"}, cbi("mini/luci", {autoapply=true}), i18n("settings"), 10)    
  24. 39      entry({"mini", "index", "logout"}, call("action_logout"), i18n("logout"))    
  25. 40  end    
  26. 41      
  27. 42  function action_logout()    
  28. 43      luci.http.header("Set-Cookie", "sysauth=; path=/")    
  29. 44      luci.http.redirect(luci.dispatcher.build_url())    
  30. 45  end    

 

这个文件定义了node ,最外面的节点,最上层菜单的显示等等。在其他的 lua 文件里,定义了其他菜单的显示和html 以及业务处理路径。每个文件对应一个菜单相。

例如 system.lua 文件

[html] view plain copy print ?
  1.  function index()    
  2. 19      luci.i18n.loadc("admin-core")    
  3. 20      local i18n = luci.i18n.translate    
  4. 21      
  5. 22      entry({"mini", "system"}, alias("mini", "system", "index"), i18n("system"), 40).index = true    
  6. 23      entry({"mini", "system", "index"}, cbi("mini/system", {autoapply=true}), i18n("general"), 1)    
  7. 24      entry({"mini", "system", "passwd"}, form("mini/passwd"), i18n("a_s_changepw"), 10)    
  8. 25      entry({"mini", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 80)    
  9. 26      entry({"mini", "system", "upgrade"}, call("action_upgrade"), i18n("a_s_flash"), 90)    
  10. 27      entry({"mini", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 100)    
  11. 28  end    

 

mudel是对应文件的, function index 定义了菜单,比如这一句entry({"mini", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 100)

1 项为菜单入口:

{"mini", "system", "reboot"}, mini 是最上层的菜单,即为用户项, system 为一个具体的菜单, reboot 为这个菜单的子菜单,如果 reboot 还需要加上子菜单的话,可以这样写:

entry({"mini", "system", "reboot", "chreboot"}, call("action_chreboot"), i18n("chreboot"), 1), 这样就会在reboot 上产生一个新的子菜单,以此类推,可以产生 N 层菜单。

第二项为菜单对应的页面,可以是lua 的源代码文件,也可以是 html 页面。

alias cgi form call 等定义了此菜单相应的处理方式, form 和 cgi 对应到 model/cbi 相应的目录下面,那里面是对应的定制好的 html 和 lua 业务处理。

alias是等同于别的链接, call 调用了相应的 action_function 。还有一种调用,是 template ,是直接链接到view 相应目录下面的 htm 页面。(说明: luci 框架下面的 htm 都是可以嵌入 lua 语句的,做业务处理,相当于 jsp 页面的内部的 Java 语句)。

问价查找对应简介:

entry({"mini", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 100)  :对应此文件的action_reboot function

entry({"mini", "system", "index"}, cbi("mini/system", {autoapply=true}), i18n("general"), 1):对应*/model/cbi/mini/system.lua  {autoapply=true}   这个失传的参数。

。。。。。

第三项为i18n 显示,比如entry({"mini", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 100),菜单的名字为admin-core 文件内的对应显示。此处也可以这样写,  i18n("reboot"," 重启 ") ,即直接做了国际化。菜单上显示的就是“重启”。

第四项为现实的顺序,这个数字越小,显示越靠前,靠上。


 

现在说一下这些文件的解析是怎么解析的呢?你当然是说dispatch.lua中,你说对了,但是真正解析成菜单的递归算法确实在header.htm中 位置:*/view/themes/openwrt/
代码如下:
[html] view plain copy print ?
  1.  <%    
  2. require("luci.sys")    
  3. local load1, load5, load15 = luci.sys.loadavg()    
  4. local request  = require("luci.dispatcher").context.path    
  5. local category = request[1]    
  6. local tree     = luci.dispatcher.node()    
  7. local cattree  = category and luci.dispatcher.node(category)    
  8. local node     = luci.dispatcher.context.dispatched    
  9. local hostname = luci.sys.hostname()    
  10. local c = tree    
  11. for i,r in ipairs(request) do    
  12.      if c.nodes and c.nodes[r] then    
  13.           c = c.nodes[r]    
  14.           c._menu_selected = true    
  15.      end    
  16. end    
  17. require("luci.i18n").loadc("default")    
  18. require("luci.http").prepare_content("application/xhtml+xml")    
  19. -%>    
  20. xml version="1.0" encoding="utf-8"?>    
  21. >    
  22. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%=luci.i18n.context.lang%>" lang="<%=luci.i18n.context.lang%>">    
  23. <head>    
  24. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
  25. <meta http-equiv="Content-Script-Type" content="text/javascript" />    
  26. <link rel="stylesheet" type="text/css" media="screen" href="<%=media%>/cascade.css" />    
  27.     
  28.     
  29. <% if node and node.css then %><link rel="stylesheet" type="text/css" media="screen" href="<%=resource%>/<%=node.css%>" />    
  30. <% end -%>    
  31. <mce:script type="text/javascript" src="<%=resource%>

你可能感兴趣的:(转载和积累系列 - luci框架-LUA的一个web框架使用)