在访问阶段动态运行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"