moved webhooks directory under server
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package tiktok
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"ruben/inventory2/internal/domains/raw_events"
|
||||
)
|
||||
|
||||
func NewWebhookHandler(logger *slog.Logger, db *raw_events.Store) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("POST /test", func(w http.ResponseWriter, r *http.Request) {
|
||||
var body json.RawMessage
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
http.Error(w, "Failed to decode body as json: "+err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
ts := time.Now().UTC()
|
||||
|
||||
err := db.Save(r.Context(), &raw_events.Event{
|
||||
Platform: "tiktok",
|
||||
StoreID: "test-store-1",
|
||||
EventID: fmt.Sprint(ts.Unix()),
|
||||
EventTimestamp: ts,
|
||||
Payload: body,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, "Error occurred saving the body as the event payload: "+err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(201)
|
||||
})
|
||||
|
||||
return mux
|
||||
}
|
||||
Reference in New Issue
Block a user