想实现这样一个功能:网页上提交一段C代码,在ngx_lua中将这段C代码保存成临时文件,并且编译,运行,返回编译与运行的结果给网页。
配置文件是这样写的:
#ngix.conf
location /compile/gcc {
lua_need_request_body on;
client_max_body_size 50k;
client_body_buffer_size 50k;
content_by_lua_file lua/test.lua;
}
test.lua的内容
#lua/test.lua
local code_file = io.open("temp.c","w")
code_file:write(code)
local ret1 = io.popen("gcc temp.c -o temp")
ngx.say(ret1:read("*all"))
local ret2 = io.popen("./temp")
ngx.say(ret2:read("*all"))
错误日志如下:
[error] 11802#0: *392 lua entry thread aborted: runtime error: /usr/local/openresty/nginx/lua/test.lua:2: attempt to index local 'code_file' (a nil value)
打开文件失败了。
想请教一下:
1.ngx_lua中是不是不能操作文件?
2.gcc能不能直接接收源码的字符串,而不用存为临时文件?
3.如果我想做一个这样的东西,各位能不能给我一些好的建议?
谢谢大家~