raw_store_events store started
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,89 @@
|
||||
package raw_events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type (
|
||||
Store struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
Event struct {
|
||||
Platform string
|
||||
StoreID string
|
||||
EventID string
|
||||
EventTimestamp time.Time
|
||||
Payload json.RawMessage
|
||||
}
|
||||
)
|
||||
|
||||
func NewStore(ctx context.Context) (*Store, error) {
|
||||
pool, err := newPool(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Store{
|
||||
db: pool,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newPool(ctx context.Context) (*pgxpool.Pool, error) {
|
||||
pool, err := pgxpool.New(ctx, "postgres://app_client:app_password@localhost:5432/inventory_2?sslmode=disable")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create database client: %w", err)
|
||||
}
|
||||
|
||||
conn, err := pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create a database connection: %w", err)
|
||||
}
|
||||
conn.Release()
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func (db *Store) Save(ctx context.Context, e *Event) error {
|
||||
_, err := db.db.Exec(
|
||||
ctx,
|
||||
`
|
||||
INSERT INTO raw_store_events (
|
||||
platform,
|
||||
store_id,
|
||||
event_timestamp,
|
||||
event_id,
|
||||
raw_payload
|
||||
)
|
||||
VALUES (
|
||||
@platform,
|
||||
@store_id,
|
||||
@event_timestamp,
|
||||
@event_id,
|
||||
@raw_payload
|
||||
)
|
||||
`,
|
||||
pgx.NamedArgs{
|
||||
"platform": e.Platform,
|
||||
"store_id": e.StoreID,
|
||||
"event_timestamp": pgtype.Timestamptz{
|
||||
Time: e.EventTimestamp,
|
||||
Valid: !e.EventTimestamp.IsZero(),
|
||||
},
|
||||
"event_id": e.EventID,
|
||||
"raw_payload": string(e.Payload),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to perform query: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,16 +1,39 @@
|
||||
package etsy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"ruben/inventory2/internal/domains/raw_events"
|
||||
)
|
||||
|
||||
func NewWebhookHandler() http.Handler {
|
||||
func NewWebhookHandler(db *raw_events.Store) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write([]byte(fmt.Sprintf("received etsy webhook call: %s %s\n", r.Method, r.URL)))
|
||||
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: "etsy",
|
||||
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
|
||||
|
||||
@@ -1,16 +1,39 @@
|
||||
package tiktok
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"ruben/inventory2/internal/domains/raw_events"
|
||||
)
|
||||
|
||||
func NewWebhookHandler() http.Handler {
|
||||
func NewWebhookHandler(db *raw_events.Store) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write([]byte(fmt.Sprintf("received tiktok webhook call: %s %s\n", r.Method, r.URL)))
|
||||
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
|
||||
|
||||
@@ -3,17 +3,18 @@ package webhooks
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"ruben/inventory2/internal/domains/raw_events"
|
||||
"ruben/inventory2/internal/webhooks/etsy"
|
||||
"ruben/inventory2/internal/webhooks/tiktok"
|
||||
"ruben/inventory2/internal/webhooks/wix"
|
||||
)
|
||||
|
||||
func New() http.Handler {
|
||||
func New(db *raw_events.Store) http.Handler {
|
||||
wh := http.NewServeMux()
|
||||
|
||||
wh.Handle("/etsy/", http.StripPrefix("/etsy", etsy.NewWebhookHandler()))
|
||||
wh.Handle("/tiktok/", http.StripPrefix("/tiktok", tiktok.NewWebhookHandler()))
|
||||
wh.Handle("/wix/", http.StripPrefix("/wix", wix.NewWebhookHandler()))
|
||||
wh.Handle("/etsy/", http.StripPrefix("/etsy", etsy.NewWebhookHandler(db)))
|
||||
wh.Handle("/tiktok/", http.StripPrefix("/tiktok", tiktok.NewWebhookHandler(db)))
|
||||
wh.Handle("/wix/", http.StripPrefix("/wix", wix.NewWebhookHandler(db)))
|
||||
|
||||
return wh
|
||||
}
|
||||
|
||||
@@ -1,16 +1,39 @@
|
||||
package wix
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"ruben/inventory2/internal/domains/raw_events"
|
||||
)
|
||||
|
||||
func NewWebhookHandler() http.Handler {
|
||||
func NewWebhookHandler(db *raw_events.Store) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write([]byte(fmt.Sprintf("received wix webhook call: %s %s\n", r.Method, r.URL)))
|
||||
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: "wix",
|
||||
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