diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f549a1d --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +so/testlib.so: src/module.c + gcc -O2 -shared -fpic -I /usr/include/luajit-2.1 -o so/clib.so src/module.c + +PHONY: run +run: + /usr/local/openresty/bin/openresty -p . \ No newline at end of file diff --git a/conf/nginx.conf b/conf/nginx.conf index 760f83b..22ac24a 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -9,15 +9,19 @@ events { http { access_log /dev/stdout; - lua_package_path "$prefix/lua/?.lua;;"; + lua_package_cpath '$prefix/so/?.so;;'; + server { listen 80; location / { default_type text/plain; content_by_lua_block { - local key_header = require "key_header" - key_header.greet("a Lua module") + local clib = require("clib") + local llib = require("llib") + + ngx.say( clib.func("Hello from") ) + ngx.say( llib.func("Hello from") ) } } } diff --git a/logs/error.log b/logs/error.log new file mode 100644 index 0000000..e69de29 diff --git a/lua/key_header.lua b/lua/key_header.lua deleted file mode 100644 index 786c6b9..0000000 --- a/lua/key_header.lua +++ /dev/null @@ -1,5 +0,0 @@ -local _M = {} -function _M.greet(name) - ngx.say("Greetings from ", name) -end -return _M diff --git a/lua/llib.lua b/lua/llib.lua new file mode 100644 index 0000000..5fa8fa2 --- /dev/null +++ b/lua/llib.lua @@ -0,0 +1,6 @@ +local _M = {} + +function _M.func(str) + ngx.say(str, " lua function") +end +return _M diff --git a/so/clib.so b/so/clib.so new file mode 100755 index 0000000..cce23b3 Binary files /dev/null and b/so/clib.so differ diff --git a/src/module.c b/src/module.c new file mode 100644 index 0000000..99039e5 --- /dev/null +++ b/src/module.c @@ -0,0 +1,19 @@ +#include +#include +#include + +static int func(lua_State *L) { + const char *arg = luaL_checkstring(L, 1); // pop argument + lua_pushfstring(L, "%s C func!", arg); // push result + return 1; // number of results +} + +static const struct luaL_Reg funcs[2] = { + {"func", func}, + {NULL, NULL} +}; + +int luaopen_clib(lua_State *L) { + luaL_newlib(L, funcs); + return 1; +}