lua 遍历linux文件夹,【Lua】Lua + openresty遍历文件目录

OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器,它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项。

今天用OpenResty + lua来遍历指定目录,返回json字符串

我们用Lua来遍历文件目录,并用nginx来访问lua文件,使其返回这个目录的json字符串。

Lua代码:

1 local lfs = require("lfs")

2

3 function getType(path)

4 return lfs.attributes(path).mode

5 end

6

7 function getSize(path)

8 return lfs.attributes(path).size

9 end

10

11 function isDir(path)

12 return getType(path) == "directory"

13 end

14

15 function findx(str,x)

16 for i = 1,#str do

17 if string.sub(str,-i,-i) == x then

18 return -i

19 end

20 end

21 end

22

23 function getName(str)

24 return string.sub(str,findx(str,"/") + 1,-1)

25 end

26

27 function getJson(path)

28 local table = "{"

29 for file in lfs.dir(path) do

30 p = path .. "/" .. file

31 if file ~= "." and file ~= ‘..‘ then

32 if isDir(p) then

33 s = "{‘text‘:‘".. file .. "‘,‘type‘:‘" .. getType(p) .. "‘,‘path‘:‘" .. p .. "‘,‘children‘:[]},"

34 else

35 s = "{‘text‘:‘".. file .. "‘,‘type‘:‘" .. getType(p) .. "‘,‘path‘:‘" .. p .. "‘,‘size‘:" .. getSize(p) .. ",‘leaf‘:true},"

36 end

37 table = table .. s

38 end

39 end

40 table = table .. "}"

41 return table

42 end

43

44 ngx.say(getJson(‘/var/www‘))

这里遍历了"/var/www"目录下的所有文件及文件夹,并将其名字、类型、大小构成json字符串返回。ngx.say是Lua透露给模块的接口。

前面已经提过如何安装配置openresty,这里就不重复了。

接下来一步,我们在我们所希望的地方新建一个文件夹作为跟目录,比如在/root下建立一个work文件夹,work文件夹下建立logs文件夹用以存放日志文件,conf文件夹用以存放nginx配置文件。

1534ccbfc0a6115d48b8075866b24165.png

然后在conf文件夹中新建一个nginx.conf配置文件。

1 worker_processes 2;

2

3 error_log logs/error.log;

4

5 events {

6 worker_connections 1024;

7 }

8 http {

9 server {

10 listen 8080;

11 server_name localhost;

12

13 location / {

14 root /root/work;

15 index index.html index.htm;

16 }

17 }

18 }

其中:

worker_processes  数字代表nginx进程数,一般设置与自己电脑cpu内核数相同

error_log  指定了日志文件存放目录

worker_connections  单个进程最大连接数

在http中添加一个server,监听8080端口,其中添加了location,之后的"/"表示访问url的最后一部分,譬如"/" ==> localhost:8080/   "/a" ==> localhost:8080/a

location内的root指定了根目录,index指定了缺省访问顺序。

如果现在访问localhost:8080,将会访问/root/work目录下的index.html文件。

我们可以用nginx -c 命令来启动指定的nginx配置文件。比如这里如果要启动nginx,命令为:

/usr/local/openresty/nginx/sbin/nginx -c /root/work/conf/nginx.conf

当然,这里必须保证8080端口没有被占用,否则就会发生:

4c0b40abcc7a3e5447048494355b78e5.png

可以用netstat -lnp | grep 8080命令来查看什么进程占用了8080端口,然后用kill命令给kill掉,最后再重启。

然后我们需要将Lua文件与nginx配合起来,在我们访问localhost:8080/dir时,将显示Lua生成的目录json字符串。

我们修改nginx.conf文件:

1 worker_processes 2;

2

3 error_log logs/error.log;

4

5 events {

6 worker_connections 1024;

7 }

8 http {

9 server {

10 listen 8080;

11 server_name localhost;

12

13 location / {

14 root /root/work;

15 index index.html index.htm;

16 }

17

18 location /dir {

19 default_type text/html;

20   content_by_lua_file /root/work/dir.lua;

21 }

22 }

23 }

当我们访问localhost:8080/dir时,将自动访问work文件夹下的dir.lua文件。

我们在/root/work目录下新建dir.lua文件,内容为上面写过的内容。

最后重启nginx

可以用下面的命令来查看nginx.conf有没有语法错误:

/usr/local/openresty/nginx/sbin/nginx -t

然后重启:

/usr/local/openresty/nginx/sbin/nginx -s reload

然后访问"localhost:8080/dir":

795ae74d3880052c99b8b0f900e29c0e.png

时间: 12-17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值