main.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "time"
  7. "git.linuxforward.com/byom/byom-golang-lib/pkg/webhook"
  8. )
  9. func main() {
  10. // Create webhook configuration
  11. cfg := webhook.Config{
  12. BaseURL: "https://api.example.com",
  13. Domain: "myapp.com",
  14. SecretKey: "your-secret-key",
  15. Timeout: 5 * time.Second,
  16. MaxRetries: 3,
  17. RetryBackoff: time.Second,
  18. }
  19. // Create new webhook client
  20. client, err := webhook.New(cfg)
  21. if err != nil {
  22. log.Fatalf("Failed to create webhook client: %v", err)
  23. }
  24. defer client.Close()
  25. // Create context with timeout
  26. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  27. defer cancel()
  28. // Send webhook
  29. err = client.Send(ctx, "user@example.com", "user.created")
  30. if err != nil {
  31. log.Fatalf("Failed to send webhook: %v", err)
  32. }
  33. fmt.Println("Webhook sent successfully!")
  34. // Example of validating a webhook signature on the receiver side
  35. payload := []byte(`{"email":"user@example.com","domain":"myapp.com","action":"user.created"}`)
  36. signature := "computed-signature-from-request-header"
  37. isValid := webhook.ValidateSignature(payload, signature, cfg.SecretKey)
  38. fmt.Printf("Webhook signature is valid: %v\n", isValid)
  39. }