您的位置:首页 > 其它

Serverless功能 - 玩转Kong插件

2019-04-19 22:42 701 查看
原文链接:https://my.oschina.net/u/1404949/blog/3039541

在访问阶段动态运行Lua代码。

启动Serverless Functions

1、在服务上启用插件

$ curl -X POST http://kong:8001/services/{service}/plugins \
--data "name=serverless-functions"  \
--data "config.functions=[]"

2、同理,也可以在路由、API上启动。

3、备注:

config.functions
: 要在访问阶段按顺序缓存和运行Lua代码数组。

插件名称

无服务器函数作为两个独立的插件出现。每一个都在插件链中以不同的优先级运行。

  • pre-function
    : 在访问阶段运行其他插件之前运行。
  • post-function
    : 在访问阶段在其他插件之后运行。

示例

1、在Kong创建一个服务:

$ curl -i -X  POST http://localhost:8001/services/ \
--data "name=plugin-testing" \
--data "url=http://httpbin.org/headers"

2、向服务添加一个路由:

$ curl -i -X  POST http://localhost:8001/services/plugin-testing/routes \
--data "paths[]=/test"

3、创建一个名为`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')

4、应用我们的Lua代码使用

pre-function
插件使用cURL文件上传:

$ curl -i -X POST http://localhost:8001/services/plugin-testing/plugins \
-F "name=pre-function" \
-F "config.functions=@custom-auth.lua"

5、测试我们的lua代码会在没有报头时终止请求:

curl -i -X GET http://localhost:8000/test

HTTP/1.1 401 Unauthorized
...
"Invalid Credentials"

转载于:https://my.oschina.net/u/1404949/blog/3039541

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: