First, FFI
FFI is LuaJIT
an extension library in which allows us to use Lua code to call C language data structures and functions.
The FFI library largely avoids the Lua/C
need to write cumbersome manual bindings in C. No need to learn a separate binding language-it parses ordinary C statements! These can be cut and pasted from C header files or reference manuals.
How to call external C library functions?
1. Load the FFI library.
2. Add a C statement to the function.
3. Call the named C function.
Look at a simple example provided by the official:
-- test_ffi.lua
local ffi = require("ffi")
ffi.cdef[[
int printf(const char *fmt, ...);
]]
ffi.C.printf("Hello %s!", "world")
run:
luajit test_ffi.lua
For details, see: http://luajit.org/ext_ffi.html
https://moonbingbing.gitbooks.io/openresty-best-practices/content/lua/FFI.html
Second, add third-party modules
The location of the default resty library:
The principle of using third-party libraries:
There are more stars, more frequent updates, more distributors
Install a third-party http library:
git clone https://github.com/ledgetech/lua-resty-http
Copy the tripartite library lib / resty / * to / opt / openresty / lulib / resty
cp lua-resty-http/lib/resty/* /opt/openresty/lualib/resty
Examples of use:
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri("http://example.com/helloworld", {
method = "POST",
body = "a=1&b=2",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
keepalive_timeout = 60,
keepalive_pool = 10
})
if not res then
ngx.say("failed to request: ", err)
return
end
ngx.status = res.status
for k,v in pairs(res.headers) do
--
end
ngx.say(res.body)