krakend lua 脚本因加载 lib 而失败

问题描述 投票:0回答:1
 "endpoint": "/apps/{id}",
      "output_encoding": "no-op",
      "method": "GET",
      "backend": [
        {
          "host": [
            "http://xxx.yyy.zz:8888"
          ],
          "url_pattern": "/apps/{id}",
          "extra_config": {
            "modifier/lua-backend": {
              "sources": [
                "/etc/krakend/Hello.lua"
              ],
              "post": "local r = response.load(); printPostBody(r:body());",
              "allow_open_libs": true
            }
          }
        }
      ]
    }, 

我的你好.lua:

local mylua = require("cjson")

function printPostBody(body)
    print(body);
end

当我添加此部分时:

local mylua = require("cjson")
我收到此错误:

2023/10/08 09:36:23 [Recovery] 2023/10/08 - 09:36:23 panic recovered:
runtime error: slice bounds out of range [-1:]
/usr/local/go/src/runtime/panic.go:153 (0xf3dc7e)
/go/pkg/mod/github.com/krakendio/[email protected]/error.go:140 (0x1971f64)
/go/pkg/mod/github.com/krakendio/[email protected]/binder.go:27 (0x1970866)
/go/pkg/mod/github.com/krakendio/[email protected]/error.go:201 (0x1972397)
/go/pkg/mod/github.com/krakendio/[email protected]/binder.go:43 (0x19707ee)
/go/pkg/mod/github.com/krakendio/[email protected]/binder.go:26 (0x1970785)
/go/pkg/mod/github.com/krakendio/krakend-lua/[email protected]/proxy/proxy.go:75 (0x1979793)
/go/pkg/mod/github.com/luraproject/lura/[email protected]/proxy/balancing.go:77 (0x13080a8)
/go/pkg/mod/github.com/luraproject/lura/[email protected]/proxy/http.go:113 (0x130db84)
/go/pkg/mod/github.com/luraproject/lura/[email protected]/router/gin/endpoint.go:50 (0x1bbf674)
/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x1bb49c1)
/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:102 (0x1bb49ac)
/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x1bb3ae6)
/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:240 (0x1bb3ac9)
/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x1bb296a)
/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:620 (0x1bb25f1)
/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:576 (0x1bb211c)
/usr/local/go/src/net/http/server.go:2936 (0x128a0b5)
/usr/local/go/src/net/http/server.go:1995 (0x1285251)
/usr/local/go/src/runtime/asm_amd64.s:1598 (0xf761a0)
lua api-gateway krakend
1个回答
0
投票

如果您使用

require
删除该行即可。您不能像这样导入 Lua 库,因为代码是虚拟执行的。

如果您想包含 JSON 解析库,您需要在

sources
下手动导入它们。

© www.soinside.com 2019 - 2024. All rights reserved.