Skip to content

Commit

Permalink
Merge pull request #64 from xataio/update-webhook-dummy-server
Browse files Browse the repository at this point in the history
Pretty print request body in dummy webhook server
  • Loading branch information
eminano authored Aug 23, 2024
2 parents 6a77103 + 4b54fbd commit ad2ba5b
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions tools/webhook/webhook_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit ad2ba5b

Please sign in to comment.