Skip to content

Commit d277fe8

Browse files
committed
added debugging feature
1 parent 55a7507 commit d277fe8

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

debug.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/bclicn/color"
7+
)
8+
9+
func debug(txt string) {
10+
if !*FlagDebug {
11+
return
12+
}
13+
fmt.Println(color.BCyan("[DEBUG]: ") + color.BLightYellow(txt))
14+
}

flags.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ var (
1616
FlagWebhookEvents = flag.String("events", "connect,disconnect,subscribe,unsubscribe", "the events to be sent to the webhook")
1717
//FlagPublishEndpoint ...
1818
FlagPublishEndpoint = flag.String("publish", "/publish", "the publish endpoint, just make it as secure as you can")
19+
//FlagDebug
20+
FlagDebug = flag.Bool("debug", false, "enable debugging mode")
1921
//Version ...
20-
Version = "2.2"
22+
Version = "2.3"
2123
//WebhookEvents ..
2224
WebhookEvents = map[string]bool{}
2325
)

server.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/gorilla/websocket"
1212
"github.com/labstack/echo"
1313
"github.com/labstack/echo/middleware"
14+
"github.com/rs/xid"
1415
)
1516

1617
var (
@@ -42,6 +43,9 @@ func WSHandler(c echo.Context) error {
4243
}
4344
})()
4445
key := c.QueryParam("key")
46+
if key == "" {
47+
key = "Anonymous#" + xid.New().String()
48+
}
4549
allowed := TriggerWebhook(Event{
4650
Action: "connect",
4751
Key: key,
@@ -63,7 +67,9 @@ func WSHandler(c echo.Context) error {
6367
}
6468
closeCh := make(chan bool)
6569
closed := false
70+
debug("New Valid Connection(" + key + ")")
6671
conn.SetCloseHandler(func(_ int, _ string) error {
72+
debug("Connection(" + key + ") has been closed (by itself)")
6773
closeCh <- true
6874
return nil
6975
})
@@ -76,13 +82,16 @@ func WSHandler(c echo.Context) error {
7682
TriggerWebhook(Event{Action: "disconnect", Key: key})
7783
case data := <-subscriber.GetMessages():
7884
msg := (data.GetPayload()).(Message)
85+
debug("Incomming message to(" + key + ") ...")
7986
if !msg.IsUserAllowed(key) {
87+
debug("The client(" + key + ") isn't allowed to see the message")
8088
continue
8189
}
8290
msg.Topic = data.GetTopic()
8391
msg.Time = data.GetCreatedAt()
8492
msg.To = nil
85-
if conn.WriteJSON(msg) != nil {
93+
if err := conn.WriteJSON(msg); err != nil {
94+
debug("A message cannot be published to (" + key + ") because of the following error (" + err.Error() + ")")
8695
closeCh <- true
8796
}
8897
}
@@ -94,9 +103,11 @@ func goRoutineAction(conn *websocket.Conn, closeCh chan bool, subscriber *pubsub
94103
go (func() {
95104
var action Event
96105
for {
97-
if conn.ReadJSON(&action) != nil {
106+
if err := conn.ReadJSON(&action); err != nil {
107+
debug("Cannot read from the connection of(" + key + "), may connection has been closed, closing ...")
98108
break
99109
}
110+
debug("An action (" + action.Action + ") from the client(" + key + ")")
100111
if action.Action == "subscribe" || action.Action == "unsubscribe" {
101112
if !TriggerWebhook(Event{Action: action.Action, Key: key, Value: action.Value}) {
102113
conn.WriteJSON(map[string]string{ // nolint: errcheck
@@ -125,6 +136,7 @@ func PublishHandler(c echo.Context) error {
125136
})
126137
}
127138
Broker.Broadcast(msg, msg.Topic)
139+
debug("publishing a message ...")
128140
return c.JSON(200, map[string]interface{}{
129141
"success": true,
130142
"data": msg,

webhook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ type Event struct {
1616

1717
// TriggerWebhook ...
1818
func TriggerWebhook(ev Event) bool {
19+
if *FlagWebhookURL == "" {
20+
return true
21+
}
1922
ev.Action = strings.ToLower(ev.Action)
2023
jdata, _ := json.Marshal(ev)
2124
reader := bytes.NewReader(jdata)

0 commit comments

Comments
 (0)