main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "log"
  8. "net/http"
  9. "time"
  10. )
  11. // WebHookNotification must match the server's definition
  12. type WebHookNotification struct {
  13. Key string `json:"key"`
  14. Value string `json:"value"`
  15. EventType int `json:"event_type"` // 0=Set, 1=Del
  16. Timestamp int64 `json:"timestamp"`
  17. }
  18. func main() {
  19. listenPort := ":9601"
  20. targetNodeAPI := "http://127.0.0.1:8501" // Node 2 API
  21. myURL := fmt.Sprintf("http://127.0.0.1%s/callback", listenPort)
  22. // Start Listener
  23. go func() {
  24. http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
  25. if r.Method != http.MethodPost {
  26. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  27. return
  28. }
  29. body, err := io.ReadAll(r.Body)
  30. if err != nil {
  31. log.Printf("Error reading body: %v", err)
  32. http.Error(w, "Bad request", http.StatusBadRequest)
  33. return
  34. }
  35. defer r.Body.Close()
  36. var event WebHookNotification
  37. if err := json.Unmarshal(body, &event); err != nil {
  38. log.Printf("Error unmarshalling JSON: %v", err)
  39. http.Error(w, "Invalid JSON", http.StatusBadRequest)
  40. return
  41. }
  42. eventTypeStr := "SET"
  43. if event.EventType == 1 {
  44. eventTypeStr = "DEL"
  45. }
  46. // Print received event
  47. fmt.Printf("\n[Client 2 Received] %s\n", time.Now().Format("15:04:05"))
  48. fmt.Printf(" Key: %s\n", event.Key)
  49. fmt.Printf(" Type: %s\n", eventTypeStr)
  50. if event.EventType == 0 {
  51. fmt.Printf(" Value: %s\n", event.Value)
  52. }
  53. fmt.Println("----------------------------------------")
  54. w.WriteHeader(http.StatusOK)
  55. })
  56. log.Printf("Client 2 Listener started on %s", listenPort)
  57. if err := http.ListenAndServe(listenPort, nil); err != nil {
  58. log.Fatalf("Server failed: %v", err)
  59. }
  60. }()
  61. // Give listener time to start
  62. time.Sleep(1 * time.Second)
  63. // Subscribe to Node 2
  64. log.Printf("Subscribing to Node 2 (%s)...", targetNodeAPI)
  65. subscribe(targetNodeAPI, "key-2", myURL)
  66. // Keep running
  67. select {}
  68. }
  69. func subscribe(apiBase, key, callbackURL string) {
  70. data := map[string]string{
  71. "key": key,
  72. "url": callbackURL,
  73. }
  74. jsonData, _ := json.Marshal(data)
  75. resp, err := http.Post(apiBase+"/watch", "application/json", bytes.NewBuffer(jsonData))
  76. if err != nil {
  77. log.Printf("Failed to subscribe: %v", err)
  78. return
  79. }
  80. defer resp.Body.Close()
  81. if resp.StatusCode == http.StatusOK {
  82. log.Printf("Successfully subscribed to key '%s'", key)
  83. } else {
  84. log.Printf("Failed to subscribe, status: %d", resp.StatusCode)
  85. }
  86. }