Lua scripting load external script.

143 views
Skip to first unread message

Emo Leumassi

unread,
Oct 8, 2023, 5:48:05 AM10/8/23
to KrakenD Community
```
 "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
            }
          }
        }
      ]
    },
```

My Hello.lua:
```
local mylua = require("cjson")

function printPostBody(body)
    print(body);
end
```

When i add this part: `local mylua = require("cjson")` i got this error:
```
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/bin...@v0.0.0-20230413105421-1bbe94e65f45/error.go:140 (0x1971f64)
/go/pkg/mod/github.com/krakendio/bin...@v0.0.0-20230413105421-1bbe94e65f45/binder.go:27 (0x1970866)
/go/pkg/mod/github.com/krakendio/bin...@v0.0.0-20230413105421-1bbe94e65f45/error.go:201 (0x1972397)
/go/pkg/mod/github.com/krakendio/bin...@v0.0.0-20230413105421-1bbe94e65f45/binder.go:43 (0x19707ee)
/go/pkg/mod/github.com/krakendio/bin...@v0.0.0-20230413105421-1bbe94e65f45/binder.go:26 (0x1970785)
/go/pkg/mod/github.com/krakendio/krakend-lua/v...@v2.1.2/proxy/proxy.go:75 (0x1979793)
/go/pkg/mod/github.com/luraproject/lura/v...@v2.3.0/proxy/balancing.go:77 (0x13080a8)
/go/pkg/mod/github.com/luraproject/lura/v...@v2.3.0/proxy/http.go:113 (0x130db84)
/go/pkg/mod/github.com/luraproject/lura/v...@v2.3.0/router/gin/endpoint.go:50 (0x1bbf674)
/go/pkg/mod/github.com/gin-gonic/g...@v1.9.1/context.go:174 (0x1bb49c1)
/go/pkg/mod/github.com/gin-gonic/g...@v1.9.1/recovery.go:102 (0x1bb49ac)
/go/pkg/mod/github.com/gin-gonic/g...@v1.9.1/context.go:174 (0x1bb3ae6)
/go/pkg/mod/github.com/gin-gonic/g...@v1.9.1/logger.go:240 (0x1bb3ac9)
/go/pkg/mod/github.com/gin-gonic/g...@v1.9.1/context.go:174 (0x1bb296a)
/go/pkg/mod/github.com/gin-gonic/g...@v1.9.1/gin.go:620 (0x1bb25f1)
/go/pkg/mod/github.com/gin-gonic/g...@v1.9.1/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)
```

Albert Lombarte

unread,
Oct 11, 2023, 3:28:02 AM10/11/23
to KrakenD Community, Emo Leumassi
Hi,

You cannot import libraries with require.

You should load any Lua script under the scripts property in the configuration.

El dia diumenge, 8 d’octubre de 2023 a les 11:48:05 UTC+2, Emo Leumassi va escriure:

Jose Augusto Batista

unread,
Jul 11, 2024, 10:03:43 AM7/11/24
to KrakenD Community, Albert Lombarte, Emo Leumassi
What would be the correct way to use it?

Daniel Lopez

unread,
Jul 11, 2024, 12:23:11 PM7/11/24
to Jose Augusto Batista, KrakenD Community, Albert Lombarte, Emo Leumassi

***

Esta mensagem, incluindo seus anexos, pode conter informações confidenciais e/ou privilegiadas. Se você não for a pessoa autorizada a receber esta mensagem, não pode usar, copiar ou divulgar as informações nela contidas ou tomar qualquer ação baseada nessas informações. Caso esta mensagem tenha sido recebida por engano, por favor avise imediatamente o remetente, respondendo o e-mail e em seguida elimine-a do seu sistema. Agradecemos sua cooperação.

This message, including any attachments, may contain confidential and/or privileged information. If you are not the addressee or authorized to receive this for the addressee, you must not use, copy, disclose or take any action based on this message or any information herein. If you have received this message in error, please advise the sender immediately by reply e-mail and delete this message.Thank you for your cooperation

--
You received this message because you are subscribed to the Google Groups "KrakenD Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to community+...@krakend.io.
To view this discussion on the web visit https://groups.google.com/a/krakend.io/d/msgid/community/3288855c-5654-435e-8436-e727395b1afdn%40krakend.io.


--
Logo
Daniel López
CTO

LinkedIn icon   Twitter icon  

Confidentiality Notice: This email, including any attachments, may contain confidential and privileged information for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure, or distribution is prohibited. If you are not the intended recipient, please contact the sender immediately and destroy all copies of this email. Thank you. This email has been sent in accordance with the European Union General Data Protection Regulation (EU GDPR).

Reply all
Reply to author
Forward
0 new messages