openwrt web开发-luci源码结构

luci解析器

luci解析器是一个web cgi程序,当浏览器发送请求时,web服务器(uhttpd)会fork luci进程,根据请求参数进行web页面渲染,渲染成功后返回html页面到浏览器。

什么是cgi

公共网关接口(Common Gateway Interface,CGI)是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展服务器功能。CGI 应用程序能与浏览器进行交互,还可通过数据API与数据库服务器等外部数据源进行通信,从数据库服务器中获取数据。格式化为HTML文档后,发送给浏览器,也可以将从浏览器获得的数据放到数据库中。几乎所有服务器都支持CGI,可用任何语言编写CGI。

luci实现

luci大部分源码通过lua语言实现,少部分库通过c语言实现,cgi主程序位于web的cgi-bin目录,以openwrt默认web目录为例,位置为/www/cgi-bin/luci

root@OpenWrt:~# ls /www/cgi-bin/ -l
-rwxr-xr-x    1 root     root         135 Jun 30 13:28 luci

入口程序只有几行代码,主要代码在库中

#!/usr/bin/lua
require "luci.cacheloader"
require "luci.sgi.cgi"
luci.dispatcher.indexcache = "/tmp/luci-indexcache"
luci.sgi.cgi.run()

luci在运行设备中的目录及文件

列表

root@OpenWrt:~# ls /usr/lib/lua/luci/ -l
drwxr-xr-x    4 root     root            54 Jun 30 13:28 advanced-reboot
-rw-r--r--    1 root     root           345 Jun 30 13:28 cacheloader.lua
drwxr-xr-x    2 root     root            36 Jun 30 13:28 cbi
-rw-r--r--    1 root     root         42590 Jun 30 13:28 cbi.lua
-rw-r--r--    1 root     root          1651 Jun 30 13:28 ccache.lua
-rw-r--r--    1 root     root           477 Jun 30 13:28 config.lua
drwxr-xr-x    1 root     root          3488 Aug 24 20:19 controller
-rw-r--r--    1 root     root           874 Jun 30 13:28 debug.lua
-rw-r--r--    1 root     root         20868 Aug 24 20:19 dispatcher.lua
-rw-r--r--    1 root     root          8670 Jun 30 13:28 fs.lua
drwxr-xr-x    3 root     root            51 Jun 30 13:28 http
-rw-r--r--    1 root     root          5722 Jun 30 13:28 http.lua
drwxr-xr-x    2 root     root           761 Jun 30 13:28 i18n
-rw-r--r--    1 root     root          1344 Jun 30 13:28 i18n.lua
-rwxr-xr-x    1 root     root         28674 Jun 30 13:28 ip.so
-rwxr-xr-x    1 root     root         16386 Jun 30 13:28 jsonc.so
-rw-r--r--    1 root     root          8954 Jun 30 13:28 ltn12.lua
drwxr-xr-x    1 root     root          3488 Jun 30 13:28 model
-rw-r--r--    1 root     root          8677 Jun 30 13:28 openclash.lua
drwxr-xr-x    2 root     root            48 Jun 30 13:28 sgi
-rw-r--r--    1 root     root           232 Jun 30 13:28 store.lua
drwxr-xr-x    3 root     root            72 Jun 30 13:28 sys
-rw-r--r--    1 root     root         11722 Jun 30 13:28 sys.lua
drwxr-xr-x    2 root     root            32 Jun 30 13:28 template
-rw-r--r--    1 root     root          2819 Jun 30 13:28 template.lua
drwxr-xr-x    2 root     root           106 Jun 30 13:28 tools
-rw-r--r--    1 root     root         17394 Jun 30 13:28 util.lua
-rw-r--r--    1 root     root           520 Jun 30 13:28 version.lua
drwxr-xr-x    1 root     root          3488 Jun 30 13:28 view
root@OpenWrt:~# 

文件说明

通过luci根目录文件列表可以看出除了lua文件外,还有部分.so文件,.so文件为linux下的动态库,用于给luci提供系统级接口,供lua调用。

开发用到的目录

在开发过程中,我们用到的有三个目录,分别为model、view、conntroller,luci是基于mvc架构实现的,代码层次比较清晰。

  • view 界面层,用于实现html相关的代码,包括模板
  • controller 控制层,实现菜单、接口的定义
  • model 数据层,实现与后台数据(uci配置)的绑定

openwrt luci源码

如何集成更新

在openwrt luci源码下载链接保存在 feeds.conf.default文件中,该配置文件用于定义第三方开源软件包,也就是openwrt源码源。 在openwrt最新官方源码中,配置为最新的luci源码路径 openwrt源码地址:

https://github.com/openwrt/openwrt.git

luci最新源码地址:

https://git.openwrt.org/project/luci.git

feeds.conf.default 内容:

src-git packages https://git.openwrt.org/feed/packages.git
src-git luci https://git.openwrt.org/project/luci.git
src-git routing https://git.openwrt.org/feed/routing.git
src-git telephony https://git.openwrt.org/feed/telephony.git

而在国内比较火的lean的源码(lede)中,配置为老版本的luci,个人私有github私有仓库地址 lede源码地址:

https://github.com/coolsnowwolf/lede.git

luci地址:

https://github.com/coolsnowwolf/luci

feeds.conf.default 内容:

src-git packages https://github.com/coolsnowwolf/packages
src-git luci https://github.com/coolsnowwolf/luci
src-git routing https://git.openwrt.org/feed/routing.git
src-git telephony https://git.openwrt.org/feed/telephony.git

更新luci源码

编译过openwrt源码的应该知道,在编译前要执行以下命令

 ./scripts/feeds update -a
 ./scripts/feeds install -a 

命令的目的为更新第三方开源包源码,并安装到编译package目录

luci在openwrt项目中的源码目录为 feeds/luci

root@ubuntu:/mnt/disk1/openwrt# ls feeds/luci -alh
total 108K
drwxr-xr-x 12 root root 4.0K Jul 17 14:16 .
drwxr-xr-x 10 root root 4.0K Jul 17 14:16 ..
drwxr-xr-x 67 root root 4.0K Jul 17 14:16 applications
drwxr-xr-x  3 root root 4.0K Jul 17 14:16 build
-rw-r--r--  1 root root  191 Jul 17 14:16 .buildpath
drwxr-xr-x  6 root root 4.0K Jul 17 14:16 collections
drwxr-xr-x  3 root root 4.0K Jul 17 14:16 contrib
-rw-r--r--  1 root root 3.9K Jul 17 14:16 CONTRIBUTING.md
-rw-r--r--  1 root root  12K Jul 17 14:16 .cproject
drwxr-xr-x  3 root root 4.0K Jul 17 14:16 documentation
drwxr-xr-x  8 root root 4.0K Jul 17 14:16 .git
-rw-r--r--  1 root root   68 Jul 17 14:16 .gitignore
drwxr-xr-x 12 root root 4.0K Jul 17 14:16 libs
-rw-r--r--  1 root root  12K Jul 17 14:16 LICENSE
-rw-r--r--  1 root root 7.1K Jul 17 14:16 luci.mk
drwxr-xr-x 10 root root 4.0K Jul 17 14:16 modules
-rw-r--r--  1 root root  356 Jul 17 14:16 NOTICE
-rw-r--r--  1 root root 2.3K Jul 17 14:16 .project
drwxr-xr-x 12 root root 4.0K Jul 17 14:16 protocols
-rw-r--r--  1 root root  849 Jul 17 14:16 README.md
-rw-r--r--  1 root root  641 Jul 17 14:16 THANKYOU
drwxr-xr-x  6 root root 4.0K Jul 17 14:16 themes
如需转载请保留该博客链接!程序员TT的博客 » openwrt web开发-luci源码结构

相关文章

评论 (0)

5 + 4 =