From b61a0e1e62151109fa7408d09932aaecff83b74b Mon Sep 17 00:00:00 2001 From: hladu357 Date: Tue, 4 Jun 2024 14:16:36 +0200 Subject: [PATCH] done --- Makefile | 6 ++++++ conf/nginx.conf | 10 +++++++--- logs/error.log | 0 lua/key_header.lua | 5 ----- lua/llib.lua | 6 ++++++ so/clib.so | Bin 0 -> 15264 bytes src/module.c | 19 +++++++++++++++++++ 7 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 Makefile create mode 100644 logs/error.log delete mode 100644 lua/key_header.lua create mode 100644 lua/llib.lua create mode 100755 so/clib.so create mode 100644 src/module.c 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 0000000000000000000000000000000000000000..cce23b3beb4ab20b5859e59148413bf6d02a16ab GIT binary patch literal 15264 zcmeHOZ)_Y#6`#ATvpw7x7E^j7zkAL8=l+f=Bn^EeO@{N3OU2NJ%^2{IQksX9}5fGZpU3&deA5=jowN> zlQjISP}ni6hu|7Hg~t@3b?GN)kBGUCe0)#**>p(sKyABav9sxb*nx5zT&CX$bML0T zqhi zg(pZCmTjVA9UZKH_0q#1x?}yrfB1Z9{Mu8g6R#|!p8XOla&znHU_WL&U1_RnjOo+A zeBtLzI~5+6vxe}Sgkj9>;;(kVk9WZ5I^f)2hYq`ZlmN#DiJzon#~S$KguPGQr54_6 zgoDuW{ki!{*{_9}YUumQKQwdL&jr6$>dfKsQl%WsWKNa>BU@YIXHRAPe6gG< z70(2!w3wOlvxOl0g;Fi77Rz&t_zR1*LcYaU7J{;$Efr5{VYV7%!XVV`X!BYS<`>J^ znj(#?TdTNx)WNAk`^WuZci0`F-qW4Y_DA=H9uZ^DQ4QB*achw-ds6*C#*6m>p2H!D z$FjU9fUk&y6${_s7)K2YPm3QXZRGF+zP``keD9f_8va@vZthEiFSX%!8c3g)+iY+48enoI|=dbD9w>?cY<_j!a zU1?I;d4+jix~?v1{zc|_skvI${3YgjiMjd$@!rMJ9}?hQ?EXHJkKG72Q`396PED_G zuJmm`OSyMma^1w~+{B`f8KkxHiy0OknyzIStGu%WDy2U|Hb0y!m{Xol} z$9Hygl*DRr*KzOM=O6k-w>n3<(!c4{Yd_<`d*}brP&~f#zxOVVe7Bd8E2(7=a|8~W)Svr10vO%Iome(?7;}S<8jzAoNI0A75;t0eMh$9e3AdWyBfj9!R zSCf20tTH4yDa^51B_Zy;C zq+#Ga!lR$dqQ|+xT+klEXSowz1g&2#n&}ScN^|Y z{XYQ0zClMq-K~&c>kyB!mY+TnCe=oT`xCmGsgj_pjs64Qp>fI#x8h;i?~C%^q0egU zXD|)K&_^|XPjufwI!l!#k51wTJWmw|OdI_Q)h$&meX;}oYzO>O2mA`*4jp!Rm4LPK zzJEu!V=EJXgAR_}ZDqcaTPy``*8Ka3bG?ho_j9$1U&xemoE?7Pvq#1b9~vi=ZXbWb z^Cypu9iH&L1IGxWL~?)P$N|nUKRET+{;?_lvB}Bli5Y)pZ2!~*q2w=o$Lw<}E1VL_ zZ_62f$CPs`(Vg72GSGF_`+tzxp1vgo-L0LTr;K&wQc}C21=PxwFmUI}i|#^|TB(Mo zl^a1P7mKCbgT!#op%f}tN6!s| zQ&iWN#;sOznJ}Z=py21LnfbskwlFZ_;9ieabkXy6y`Z}6P$({A7Cdm8A=Ya6T@quN{a z4oCaZgd-ErfM~+bZvT@c=Q#`ZnAbqBi#^W|2|8ep`DU6hZVL98CqXZhti7<0_Hu$4 zo}*xoc?p!ik>P^=Ll3k-GM@9mW8MRG*qDmle}gK;-(i1zLAKOy$} zMGx~f=xm$)Jyf^tFA&Bt!MY-P*~&{7>lbko#R2@+Eqh1oK#xLEsEDIxp0ey0L;-Zr zx?r$}+izL+nCC#TK7&5u2>v@_A5GJaNQBz$p*x_@i#@JiKkS4Kki5Ov?f-~`>L$~T1uJ!zZ=Y=2p!uYX{8th;%;efg7B7iYb zk8s%A&oAuTDek?P +#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; +}