upda/server/api_handler_webhook_invocation.go
Varakh 1fc3818d3c
All checks were successful
/ build (push) Successful in 5m3s
feature(api,release): prepare for next major release and switch to requiring content-type set to JSON for all incoming requests and expose more CORS environment variables
- Switched to enforce JSON as Content-Type for all incoming requests
- Switched to properly respond with JSON on page not found or method not allowed
- Renamed CORS_ALLOW_ORIGIN to CORS_ALLOW_ORIGINS
- Added CORS_ALLOW_CREDENTIALS which defaults to true
- Added CORS_EXPOSE_HEADERS which defaults to *
- Overhauled package visibility for server module
2024-06-10 20:03:25 +02:00

62 lines
1.6 KiB
Go

package server
import (
"errors"
"git.myservermanager.com/varakh/upda/api"
"github.com/gin-gonic/gin"
"net/http"
)
type webhookInvocationHandler struct {
invocationService webhookInvocationService
webhookService webhookService
}
func newWebhookInvocationHandler(i *webhookInvocationService, w *webhookService) *webhookInvocationHandler {
return &webhookInvocationHandler{invocationService: *i, webhookService: *w}
}
func (h *webhookInvocationHandler) execute(c *gin.Context) {
tokenHeader := c.GetHeader(headerWebhookToken)
webhookId := c.Param("id")
var w *Webhook
var err error
if w, err = h.webhookService.get(webhookId); err != nil {
_ = c.AbortWithError(errToHttpStatus(err), err)
return
}
switch w.Type {
case api.WebhookTypeGeneric.Value():
var req api.WebhookGenericRequest
if err = c.ShouldBindJSON(&req); err != nil {
errAbortWithValidatorPayload(c, err)
return
}
if err = h.invocationService.executeGeneric(webhookId, tokenHeader, req); err != nil {
_ = c.AbortWithError(errToHttpStatus(err), err)
return
}
break
case api.WebhookTypeDiun.Value():
var req api.WebhookDiunRequest
if err = c.ShouldBindJSON(&req); err != nil {
errAbortWithValidatorPayload(c, err)
return
}
if err = h.invocationService.executeDiun(webhookId, tokenHeader, req); err != nil {
_ = c.AbortWithError(errToHttpStatus(err), err)
return
}
break
default:
err = newServiceError(illegalArgument, errors.New("no default handler for webhook type found"))
_ = c.AbortWithError(errToHttpStatus(err), err)
return
}
c.Header(headerContentType, headerContentTypeApplicationJson)
c.Status(http.StatusNoContent)
}