zoukankan      html  css  js  c++  java
  • 【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,这里就不重复了。

      接下来一步,我们在nginx下新建一个文件夹作为跟目录,比如在/usr/local/openresty/nginx/下建立一个work文件夹,work文件夹下建立logs文件夹用以存放日志文件,conf文件夹用以存放nginx配置文件。

      

      然后在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 /usr/local/openresty/nginx/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,将会访问/usr/local/openresty/nginx/work目录下的index.html文件。

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

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

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

      

      可以用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 /usr/local/openresty/nginx/work;
    15                index  index.html index.htm;     
    16        }
    17        
    18        location /dir {
    19                default_type text/html;
    20               content_by_lua_file /usr/local/openresty/nginx/work/dir.lua;
    21        }
    22     }
    23 }

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

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

      最后重启nginx。

      关于重启nginx,有两种方法,一种是用nginx -s reload,还有一种是kill掉nginx进程后,再nginx -c XXX/nginx.conf来启动指定nginx配置文件。

      两者的区别在于,nginx -s 和nginx -t缺省判断nginx默认的配置文件,即:

        /usr/local/openresty/nginx/sbin/nginx -t [-s reload]

      命令判断与重载的配置文件为默认的/usr/local/openresty/nginx/conf/nginx.conf文件。

      而第二种启动方法则可以启动指定目录下的配置文件,不过在启动前,需要保证没有nginx进程存在,所以一般需要先kill掉所有nginx进程,再用XXX/nginx -c XXX/nginx.conf来根据指定配置文件启动nginx。

      这里我们用第二种方法启动nginx:

    killall -9 nginx
    /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/work/conf/nginx.conf

      启动成功后访问"localhost:8080/dir":

  • 相关阅读:
    codevs1430 素数判定
    codevs1212 最大公约数
    codevs1012 最大公约数和最小公倍数问题
    codevs1160 蛇形矩阵
    Debate CodeForces
    Divide Candies CodeForces
    Login Verification CodeForces
    Colorful Bricks CodeForces
    ExaWizards 2019 English D
    Cards and Joy CodeForces
  • 原文地址:https://www.cnblogs.com/linxiong945/p/4170012.html
Copyright © 2011-2022 走看看