在訪問階段動態運行Lua代碼。數組
一、在服務上啓用插件緩存
$ curl -X POST http://kong:8001/services/{service}/plugins \ --data "name=serverless-functions" \ --data "config.functions=[]"
二、同理,也能夠在路由、API上啓動。服務器
三、備註:
config.functions
: 要在訪問階段按順序緩存和運行Lua代碼數組。less
無服務器函數做爲兩個獨立的插件出現。每個都在插件鏈中以不一樣的優先級運行。curl
pre-function
: 在訪問階段運行其餘插件以前運行。post-function
: 在訪問階段在其餘插件以後運行。一、在Kong建立一個服務:函數
$ curl -i -X POST http://localhost:8001/services/ \ --data "name=plugin-testing" \ --data "url=http://httpbin.org/headers"
二、向服務添加一個路由:post
$ curl -i -X POST http://localhost:8001/services/plugin-testing/routes \ --data "paths[]=/test"
三、建立一個名爲`custom-auth.lua``的文件,內容以下:測試
-- 獲取請求頭部列表 local custom_auth = kong.request.get_header("x-custom-auth") -- 若是咱們沒有自定義頭部 if not custom_auth then return kong.response.exit(401\, "Invalid Credentials") end -- 從請求中刪除自定義身份驗證頭部 kong.service.request.clear_header('x-custom-auth')
四、應用咱們的Lua代碼使用pre-function
插件使用cURL文件上傳:lua
$ curl -i -X POST http://localhost:8001/services/plugin-testing/plugins \ -F "name=pre-function" \ -F "config.functions=@custom-auth.lua"
五、測試咱們的lua代碼會在沒有報頭時終止請求:url
curl -i -X GET http://localhost:8000/test HTTP/1.1 401 Unauthorized ... "Invalid Credentials"