From 4b54fbd2407c2d6649a437d3988d56376151c382 Mon Sep 17 00:00:00 2001 From: eminano Date: Fri, 23 Aug 2024 10:59:40 +0200 Subject: [PATCH] Pretty print webhook request body --- tools/webhook/webhook_server.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tools/webhook/webhook_server.go b/tools/webhook/webhook_server.go index 70f0c6d..f2327d5 100644 --- a/tools/webhook/webhook_server.go +++ b/tools/webhook/webhook_server.go @@ -3,16 +3,17 @@ package main import ( + "bytes" "encoding/json" "flag" "fmt" + "io" "net/http" "os" "time" "github.com/xataio/pgstream/internal/log/zerolog" loglib "github.com/xataio/pgstream/pkg/log" - "github.com/xataio/pgstream/pkg/wal/processor/webhook" ) var logger loglib.Logger @@ -51,18 +52,19 @@ func processWebhook(w http.ResponseWriter, r *http.Request) { logger.Debug("got /webhook request") - payload := webhook.Payload{} - if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { - w.WriteHeader(http.StatusBadRequest) + bodyBytes, err := io.ReadAll(r.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) return } + defer r.Body.Close() - if payload.Data != nil { - logger.Debug("webhook request payload received", loglib.Fields{ - "event_action": payload.Data.Action, - "event_schema": payload.Data.Schema, - "event_table": payload.Data.Table, - }) + var prettyJSON bytes.Buffer + if err = json.Indent(&prettyJSON, bodyBytes, "", " "); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return } + logger.Info(prettyJSON.String()) + w.WriteHeader(http.StatusOK) }