| 15 | const coreKey = "_chanify/http/core" |
| 16 | |
| 17 | func (c *Core) handlePostWebhook(ctx *gin.Context) { |
| 18 | name := strings.ToLower(ctx.Param("name")) |
| 19 | webhook, err := c.logic.GetWebhook(name) |
| 20 | if err != nil { |
| 21 | ctx.JSON(http.StatusNotFound, gin.H{"res": http.StatusNotFound, "msg": "no webhook found"}) |
| 22 | return |
| 23 | } |
| 24 | ctx.Set(coreKey, c) |
| 25 | if ctx.Request.Body != nil { |
| 26 | if body, err := io.ReadAll(ctx.Request.Body); err == nil { |
| 27 | ctx.Set(gin.BodyBytesKey, body) |
| 28 | } |
| 29 | } |
| 30 | l := lua.NewState() |
| 31 | defer l.Close() |
| 32 | initHttpLua(l, ctx) |
| 33 | if err := webhook.DoCall(l); err != nil { |
| 34 | ctx.JSON(http.StatusBadRequest, gin.H{"res": http.StatusBadRequest, "msg": err.Error()}) |
| 35 | return |
| 36 | } |
| 37 | code, ctype, data := getHttpLuaReturn(l) |
| 38 | ctx.DataFromReader(code, int64(len(data)), ctype, strings.NewReader(data), map[string]string{}) |
| 39 | } |
| 40 | |
| 41 | func initHttpLua(l *lua.LState, ctx *gin.Context) { |
| 42 | l.SetField(l.NewTypeMetatable("Request"), "__index", l.SetFuncs(l.NewTable(), luaRequestMethods)) |