码迷,mamicode.com
首页 > 其他好文 > 详细

【Lua】Lua + openresty遍历文件目录

时间:2014-12-18 00:01:12      阅读:861      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   sp   for   

  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配置文件。

  bubuko.com,布布扣

  然后在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端口没有被占用,否则就会发生:

  bubuko.com,布布扣

  可以用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":

bubuko.com,布布扣

 

【Lua】Lua + openresty遍历文件目录

标签:style   blog   http   ar   io   color   os   sp   for   

原文地址:http://www.cnblogs.com/linxiong945/p/4170012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!