Hello,
i have try to uses openresty for api and client side
_javascript_ development, for code to be more readable js code is
separated into multiple files, but in order to live test the code the js
need to combine into single dev.js file in order to simplify
development. i know it can be handle using other tool such nodejs gaze,
but if will be more simpler if only openresty is required.
in index.html
<script src="dev.js"></script>
in nginx.conf
location /dev.js {
default_type "application/_javascript_; charset=UTF-8";
content_by_lua_file "api/dev.lua";
}
dev.lua
local popen = io.popen
local open = io.open
local say = ngx.say
local path = 'app'
local cmd = 'find '.. path ..' -name "*.js"'
local handler,err = popen(cmd)
if not handler then return end
for file in handler:lines() do
local h = open(file,'r')
say(h:read("*all"))
h:close()
end
handler:close()
this
code often get interupt system call error, i have read few about
blocking nature of io.popen in openresty which cause error interrupt
system call. so i have reduce io access by changes multiple file open
handler into subrequest instead
dev.lua
local popen = io.popen
local open = io.open
local say = ngx.say
local capture = ngx.location.capture
local path = 'app'
local cmd = 'find '.. path ..' -name "*.js"'
local handler,err = popen(cmd)
if not handler then return end
local result = handler:read('*a')
handler:close()
if not result then ngx.log(ngx.WARN, "files not found") return end
local files = split(result,'\n') -- using custom string split function
for i=1, #files in do
local file = files[i]
local res = capture('/'.. file)
if res.status == 200 then say(res.body) end
end
the
interrupt system call error gone, but the popen call often return nil
result. the core function need to achieve actually how to get certain
files in folder which later can be combined.
is there any other
way to achieve this. trying using lua ffi to uses posix opendir, readdir
to reduce io issue with openresty without luck aside of its cross
platform issue, which we have to implement for every possible platform.
or is there any way we can get list of files in directory like
auto_index module and get the value in lua so we can filter and combine
it.
regard
Anton Heryanto