一、FFI
FFIは LuaJIT
、Luaコードを使用してC言語のデータ構造と関数を呼び出すことができる拡張ライブラリです。
FFIライブラリは、面倒な手動Lua/C
バインディングをCで作成する必要性を大幅に回避します。 個別のバインディング言語を学ぶ必要はありません-通常のCステートメントを解析します!これらは、Cヘッダーファイルまたはリファレンスマニュアルからカットアンドペーストできます。
外部Cライブラリ関数を呼び出す方法は?
1. FFIライブラリをロードします。
2.関数にCステートメントを追加します。
3.名前付きC関数を呼び出します。
公式が提供する簡単な例を見てください:
-- test_ffi.lua
local ffi = require("ffi")
ffi.cdef[[
int printf(const char *fmt, ...);
]]
ffi.C.printf("Hello %s!", "world")
実行:
luajit test_ffi.lua
詳細については、http://luajit.org/ext_ffi.htmlを参照してください。
https://moonbingbing.gitbooks.io/openresty-best-practices/content/lua/FFI.html
次に、サードパーティのモジュールを追加します
デフォルトのRestyライブラリの場所:
サードパーティのライブラリを使用する原則:
より多くのスター、より頻繁なアップデート、より多くのディストリビューターがあります
サードパーティのhttpライブラリをインストールします。
git clone https://github.com/ledgetech/lua-resty-http
三者ライブラリlib / resty / *を/ opt / openresty / lulib / restyにコピーします
cp lua-resty-http/lib/resty/* /opt/openresty/lualib/resty
使用例:
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)